Keras:输入层并正确传递输入数据

时间:2018-07-14 19:35:48

标签: python machine-learning keras nlp classification

我正在学习使用Keras功能API,并且已经设法建立和编译模型。但是,当我调用model.fit传递数据X和标签y时,出现了错误。看来我还不知道它是如何工作的。

任务是将句子分为6种类型,代码如下:

X_ = ... # shape: (2787, 100) each row a sentence and each column a feature
y_= ... # shape: (2787,)

word_matrix_weights= ... # code to initiate a lookup matrix for vocabulary embeddings. shape: (9825,300)

deep_inputs = Input(shape=(100,))
embedding = Embedding(9825, 300, input_length=100,
                      weights=[word_matrix_weights], trainable=False)(deep_inputs)
flat = Flatten()(embedding)
hidden = Dense(6, activation="softmax")(flat)

model = Model(inputs=deep_inputs, outputs=hidden)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(x=X_,y=y_,epochs=100, batch_size=10, verbose=0) #error here

最后一行会产生错误:

  File "/home/zz/Programs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1555, in fit
    batch_size=batch_size)
  File "/home/zz/Programs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1413, in _standardize_user_data
    exception_prefix='target')
  File "/home/zz/Programs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 154, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking target: expected dense_1 to have shape (None, 6) but got array with shape (2878, 1)

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您有一个包含6个单位的密集层,最后一层是softmax激活。因此,其输出将为(?,6)形状,其中这6个值中的每一个都表示属于相应类别的概率。由于您已使用categorical_crossentropy作为损失函数,因此标签(即y_)也应具有相同的形状(即(2787,6))。您可以使用to_categorical方法对y_进行一次热编码:

from keras.utils import to_categorical

y_ = to_categorical(y_)

此一键编码标签,即将3转换为[0,0,0,1,0,0](假设标签编号从零开始)。

如果您不想一次性编码标签,可以将loss参数更改为'sparse_categorical_crossentropy'