【问题标题】:Error is output layer shape in neural network, can't figure out the mistake in dimensions错误是神经网络中的输出层形状,无法弄清楚维度的错误
【发布时间】: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_xtest_x是什么?

标签: python numpy keras deep-learning


【解决方案1】:

我可能是错的,但是您是否也在您的测试数据上使用了 to_categorical,您要使用它来进行评估?

y_test = to_categorical(y_test,num_classes)

【讨论】:

  • 我刚刚运行了我的一个旧模型并错过了这部分 - 得到了一个非常相似的消息 - 检查目标时出错:预期 dense_2 的形状为 (10,) 但数组的形状为 (1, )
  • num_classes 可能是 3 - 我在这个例子中使用了 10 个类
猜你喜欢
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 2020-03-16
  • 2020-02-10
  • 1970-01-01
相关资源
最近更新 更多