【发布时间】:2018-05-15 08:09:04
【问题描述】:
我从神经网络开始。我已经建立了一个模型,并在我的训练数据集上对其进行了训练。但是当我尝试评估模型时,我得到一个尺寸不匹配错误:ValueError: Error when checking target: expected dense_16 to have shape (3,) but got array with shape (1,)。对于我的生活,似乎无法弄清楚它在模型定义中的来源。任何帮助表示赞赏。
代码:
# Building a model - build a simple feedforward neural network for this problem.
# Specify all the parameters we will be using in our network
input_num_units = (32, 32, 3)
hidden_num_units = 1000
output_num_units = 3
epochs = 10
batch_size = 128
from keras.models import Sequential
from keras.layers import Dense, Flatten, InputLayer
# Define the network/model
model = Sequential([
InputLayer(input_shape=input_num_units),
Flatten(),
Dense(units=hidden_num_units, activation='relu'),
Dense(units=output_num_units, activation='softmax'),
])
model.summary()
# Compile our network and let it train for a while, with cross validation
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_x, train_y, batch_size=batch_size,epochs=epochs,verbose=1, validation_split=0.2)
score = model.evaluate(test_x, test_y, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
【问题讨论】:
-
我觉得你应该检查一下 test_x 的形状。
-
最重要的一块不见了:
train_x和test_x是什么?
标签: python numpy keras deep-learning