简体   繁体   English

Keras 不兼容的形状

[英]Keras incompatible shapes

I have a training and testing set with the following dimensions:我有一个具有以下维度的训练和测试集:

X_train.shape = (1,10767,11) and y_train.shape = (1,10767,3)

I am trying to implement a CNN in order to predict y_train.我正在尝试实现一个 CNN 来预测 y_train。 My model's architecture is as follows:我的模型架构如下:

model = keras.models.Sequential()

model.add(Conv1D(32, kernel_size=5, strides=2, activation='relu', input_shape= (None,11)))
model.add(Conv1D(64, kernel_size=2, strides=1, activation='relu'))       
model.add(Dense(64, activation='relu'))
model.add(Dense(3, activation='sigmoid'))   # Final Layer using Softmax
epochs = 10
lrate = 0.01
decay = lrate/epochs
sgd = SGD(lr=lrate,momentum=0.9,decay=decay, nesterov=False)
model.compile(loss='mean_squared_error', optimizer='sgd', metrics = ['mse'])

However, when I use fit, after my first epoch prints, I get the following error:但是,当我使用 fit 时,在我的第一个 epoch 打印后,我收到以下错误:

Incompatible shapes: [1,5381,3] vs. [1,10767,3]不兼容的形状:[1,5381,3] 与 [1,10767,3]

I have tried to add a Flatten layer before my last dense layer, however, the problem is that the shape is not fully defined, making me change my input_shape to (10767,11).我试图在最后一个密集层之前添加一个 Flatten 层,但是,问题是形状没有完全定义,使我将 input_shape 更改为 (10767,11)。 However, while fitting I still obtain the error:但是,在拟合时,我仍然会收到错误:

expected dense_99 to have 2 dimensions, but got array with shape (1, 10767, 3)预计 dense_99 有 2 个维度,但得到了形状为 (1, 10767, 3) 的数组

which is my last dense layer.这是我的最后一个密集层。

If I try to reduce dimensions in the input shape and in my data, it will say that it expected a dimension of 3 and I gave a dimension of 2.如果我尝试减少输入形状和数据中的维度,它会说它期望维度为 3,而我给出的维度为 2。

I believe the issue with the the y_train data shape.我相信 y_train 数据形状的问题。 I modified the shape to be:我将形状修改为:

y_train=np.ones((1,5381,3))

and did not get errors.并没有得到错误。 Since you are striding by 2 in the first convolution layer, the number of 1D steps is reduced by factor of 2 (minus the kernel size of 5) throughout the network, it appears.由于您在第一个卷积层中跨步为 2,因此在整个网络中,一维步骤的数量减少了 2 倍(减去 kernel 大小为 5),它出现了。 I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM