我想对我的 29 个特征输入数据(如 29x1 形状)应用一维卷积。我告诉 Keras input_shape=(29,1)
但是我得到一个错误,它期望输入“有 3 个维度,但得到了形状为 (4000, 29) 的数组”。 为什么 Keras 需要 3 个维度?
Keras 文档给出了这个关于如何使用 input_shape 的奇怪例子:
(None, 128) for variable-length sequences with 128 features per step.
我不确定它们所说的可变长度序列是什么意思,但由于我有 29 个特征,我也尝试过
(None,29)
和 (1,29)
并得到了类似的错误。我对一维卷积的作用有误解吗?
这是我期望 Conv1D 在内核大小为 3 的情况下的视觉描述,给定 7x1 输入。
[x][x][x][ ][ ][ ][ ]
[ ][x][x][x][ ][ ][ ]
[ ][ ][x][x][x][ ][ ]
[ ][ ][ ][x][x][x][ ]
[ ][ ][ ][ ][x][x][x]
最佳答案
为什么 Keras 需要 3 个维度?
The three dimensions are (batch_size, feature_size, channels).
定义一维卷积层Conv1D(32, (3), activation='relu' , input_shape=( 29, 1 ))
将 (4000, 29, 1) 个样本馈送到该层。
简单的例子:from keras import models, layers import numpy as np x = np.ones((10, 29, 1)) y = np.zeros((10,)) model = models.Sequential() model.add(layers.Conv1D(32, (3), activation='relu' , input_shape=( 29,1))) model.add(layers.Flatten()) model.add(layers.Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer= "adam", metrics=['accuracy']) print(model.summary()) model.fit(x,y)
关于tensorflow - Keras/Tensorflow Conv1D 预期输入形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55958592/