我有一个大小为100乘50(100个50D特征)的数据矩阵来自5个类。我通过将数据矩阵重塑为
来将每个特征视为图像(1乘50像素)X=X.reshape(X.shape[0],1,X.shape[1],1)
因此,我的输入形状将是
inpshape= (1,1, X.shape[1])
接下来,我将CNN定义为
# build model
model.add(Conv2D(32, (3, 3), padding='same',input_shape=inpshape ))
model.add(Activation('relu'))
.
.
.
然而我收到错误
Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=2
我对1D数据的处理是否为2D错误? 如果是,我如何使用我的数据实现1D转换网络。
----------------------更新------------------------ ------------- 这是我用1转换层编写的代码:
from keras.layers import Conv2D, GlobalMaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.optimizers import Adam
num_labels = y.shape[1]
X=X.reshape(X.shape[0],1,X.shape[1],1)
inpshape= (1,1, X.shape[1])
print(X.shape)
# build model
model.add(Conv2D(32, (1, 3), padding='same',input_shape=inpshape ))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(300))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_labels))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'],optimizer='Adam')
错误:输入0与图层conv2d_1不兼容:预期ndim = 4,发现ndim = 2
答案 0 :(得分:0)
在添加图层model= Sequential()
看起来您正在为已经训练过的模型添加图层。