Python 神经网络维数不匹配

Python 神经网络维数不匹配,python,numpy,machine-learning,neural-network,keras,Python,Numpy,Machine Learning,Neural Network,Keras,我为Keras中的MNIST digits数据集设置了一个神经网络,如下所示: input_size = features_train.shape[1] hidden_size = 200 output_size = 9 lambda_reg = 0.2 learning_rate = 0.01 num_epochs = 50 batch_size = 30 model = Sequential() model.add(Dense(input_size, hidden_size, W_regu

我为Keras中的MNIST digits数据集设置了一个神经网络,如下所示:

input_size = features_train.shape[1]
hidden_size = 200
output_size = 9
lambda_reg = 0.2
learning_rate = 0.01
num_epochs = 50
batch_size = 30

model = Sequential()
model.add(Dense(input_size, hidden_size, W_regularizer=l2(lambda_reg), init='normal'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))

model.add(Dense(hidden_size, output_size, W_regularizer=l2(lambda_reg), init='normal'))
model.add(Activation('softmax'))

sgd = SGD(lr=learning_rate, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)

history = History()

model.fit(features_train, labels_train, batch_size=batch_size, nb_epoch=num_epochs, show_accuracy=True, verbose=2, validation_split=0.2, callbacks=[history])
score = model.evaluate(features_train, labels_train, show_accuracy=True, verbose=1)
predictions = model.predict(features_train)
print('Test score:', score[0])
print('Test accuracy:', score[1])
特征是形状(1000784),标签是(1000,1),两者都是numpy阵列。我需要784个输入节点、200个隐藏节点和9个输出节点来对数字进行分类

我不断收到输入维度不匹配错误:

Input dimension mis-match. (input[0].shape[1] = 9, input[1].shape[1] = 1)
Apply node that caused the error: Elemwise{Sub}[(0, 0)](AdvancedSubtensor1.0, AdvancedSubtensor1.0)
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(30L, 9L), (30L, 1L)]
Inputs strides: [(36L, 4L), (4L, 4L)]
Inputs values: ['not shown', 'not shown']

我试图找出我的尺寸可能不正确的地方,但我没有看到。有人看到这个问题了吗?

我已经训练了两个类别分类模型很长时间,以至于我习惯于处理仅仅是单个值的标签。对于这个问题(将多个结果分类),我只需将标签更改为向量本身

这解决了我的问题:

from keras.utils.np_utils import to_categorical

labels_train = to_categorical(labels_train)

输出大小应为10。你有0-9个数字