我不断收到此错误:
ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (1, 10, 1)
但是我指定density_3为1维,这是我的代码:
X_train=X_train.reshape(1,10,200,200)
y_train=y_train.reshape(1,10,1)
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(10,200,200)))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, y_train,
batch_size=3, epochs=100, verbose=1)
即使将Y数据更改为2维,它也不起作用,我得到:
ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (1, 10, 2)
我最不了解的事情是,在另一个项目中,我做了同样的事情并且奏效了。
答案 0 :(得分:0)
您会看到model.summary()
,除了输出的形状是(?,1)
。但是您的y_train
形状是(1,10,1)
。
因此,您可以根据需要将y_train
调整为(?,1)
或调整模型以匹配输入。
print(model.summary())
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 8, 198, 32) 57632
_________________________________________________________________
conv2d_2 (Conv2D) (None, 6, 196, 32) 9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 3, 98, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 3, 98, 32) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 9408) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 1204352
_________________________________________________________________
dense_2 (Dense) (None, 64) 8256
_________________________________________________________________
dropout_2 (Dropout) (None, 64) 0
_________________________________________________________________
dense_3 (Dense) (None, 1) 65
=================================================================
Total params: 1,279,553
Trainable params: 1,279,553
Non-trainable params: 0
_________________________________________________________________
修改
如果除2个尺寸外,应更改Flatten()
层和模型结构。因为我不知道您需要什么网络结构,所以无法为您进行更正。当然,您也可以将y_train
保留为(1,10,1)
。尝试遵循它。
model.add(Dense(10, activation='sigmoid'))
model.add(Reshape((10,1)))
我建议您在原始结构下修改y_train
。您可以将y_train
完全更改为(?,10)
。
# shape=(?,10)
y_train=y_train.reshape(1,10)
# change shape
model.add(Dense(10, activation='sigmoid'))