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?