【发布时间】:2019-12-23 11:41:07
【问题描述】:
我在编译 Keras Sequential 模型时遇到以下错误:
ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 45985 arrays: [array([[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
...
这是我用于 X_train、y_train、X_test 和 y_test 的代码和数据格式:
print(X_train.shape)
>>(45985, 50, 50, 3)
print(X_test.shape)
>>(22650, 50, 50, 3)
print(y_train[0])
>>array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.])
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation="relu", input_shape=(50,50,3)))
model.add(Conv2D(32, kernel_size=3, activation="relu"))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=3)
【问题讨论】:
-
每个数组的 dtype (X_train.dtype) 是什么?
-
X_train.dtype---> dtype('float64')
-
您的输入形状是 xtrain 的 (45985, 50, 50, 3),而 keras 的输入是 50,50,3,这是导致错误的原因
-
@JaskaranSingh 我不确定在这种情况下如何准确更改代码。你介意澄清一下吗?
-
@JaskaranSingh 不,错了,错误指向目标。
标签: python keras computer-vision classification conv-neural-network