Python:keras形状不匹配错误

Seg*_*ted 3 python keras

我正在尝试构建一个非常简单的多层感知器(MLP)keras:

model = Sequential()
model.add(Dense(16, 8, init='uniform', activation='tanh'))
model.add(Dropout(0.5))
model.add(Dense(8, 2, init='uniform', activation='tanh'))

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

model.fit(X_train, y_train, nb_epoch=1000, batch_size=50)
score = model.evaluate(X_test, y_test, batch_size=50)
Run Code Online (Sandbox Code Playgroud)

我的训练数据形状:X_train.shape给出(34180, 16)

标签属于具有shape:y_train.shapegive的二进制类(34180,)

所以我的keras代码应该生成具有以下连接的网络:16x8 => 8x2

这会产生形状不匹配错误:

ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 1)

Apply node that caused the error: Elemwise{sub,no_inplace}(Elemwise{Composite{tanh((i0 + i1))}}[(0, 0)].0, <TensorType(float64, matrix)>)
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(50, 2), (50, 1)]
Inputs strides: [(16, 8), (8, 8)]
Run Code Online (Sandbox Code Playgroud)

Epoch 0在行model.fit(X_train, y_train, nb_epoch=1000, batch_size=50).我是否在监督Keras中显而易见的事情?

编辑:我在这里经历了这个问题,但没有解决我的问题

mat*_*dns 10

我有同样的问题,然后发现这个线程;

https://github.com/fchollet/keras/issues/68

您似乎要说明最终输出层为2或任何数量的类别,标签需要是分类类型,其中基本上这是每个观察的二元向量,例如3类输出向量[0,2,1, 0,1,0]变为[[1,0,0],[0,0,1],[0,1,0],[1,0,0],[0,1,0],[1 ,0,0].

np_utils.to_categorical函数为我解决了这个问题;

from keras.utils import np_utils, generic_utils

y_train, y_test = [np_utils.to_categorical(x) for x in (y_train, y_test)]
Run Code Online (Sandbox Code Playgroud)