ValueError: Error when checking target: expected dense_3 to have 2 dimensions. But I specified dense_3 to have 1 dimension
ValueError: Error when checking target: expected dense_3 to have 2 dimensions. But I specified dense_3 to have 1 dimension
我不断收到此错误:
ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (1, 10, 1)
但我指定 dense_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 数据更改为二维,它也不起作用,我得到:
ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (1, 10, 2)
我最不明白的是,在另一个项目中,我做了同样的事情并且成功了。
可以看到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)))
建议大家修改下原来的structure.You下的y_train
可以完全把y_train
改成(?,10)
.
# shape=(?,10)
y_train=y_train.reshape(1,10)
# change shape
model.add(Dense(10, activation='sigmoid'))
我不断收到此错误:
ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (1, 10, 1)
但我指定 dense_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 数据更改为二维,它也不起作用,我得到:
ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (1, 10, 2)
我最不明白的是,在另一个项目中,我做了同样的事情并且成功了。
可以看到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)))
建议大家修改下原来的structure.You下的y_train
可以完全把y_train
改成(?,10)
.
# shape=(?,10)
y_train=y_train.reshape(1,10)
# change shape
model.add(Dense(10, activation='sigmoid'))