Keras:输入层并正确传递输入数据 [英] Keras: Input layer and passing input data correctly

查看:1030
本文介绍了Keras:输入层并正确传递输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am learning to use Keras functional API and I have managed to build and compile a model. But when I call the model.fit passing the data X and labels y, I got an error. It seems I still haven't got the idea of how it works.

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

The task is classifying sentences into 6 types, and the code goes:

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)

有什么建议吗?

推荐答案

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

You have a Dense layer with 6 units and softmax activation as the last layer. So its output would be of shape (?,6) where each of those 6 values indicates the probability of belonging to corresponding class. Since you have used categorical_crossentropy as the loss function, the labels (i.e. y_) should have the same shape (i.e. (2787,6)) as well. You can one-hot encode y_ by using to_categorical method:

from keras.utils import to_categorical

y_ = to_categorical(y_)

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

This one-hot encodes the labels, i.e. converts 3 to [0,0,0,1,0,0] (assuming label numbers start from zero).

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

If you don't want to one-hot encode your labels you can change the loss argument to 'sparse_categorical_crossentropy'.

这篇关于Keras:输入层并正确传递输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆