Keras: Input layer and passing input data correctl

2019-08-18 03:58发布

问题:

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.

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

The last line generates an error:

  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)

Any suggestions, please?

回答1:

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_)

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

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