I am trying to implement a keras LSTM. I am getting an error inside keras.model.fit. I am not understanding what this error means. My code is given below -
print(x_train.shape)
print(y_train.shape)
word_input = Input(shape=(mxlen,), dtype="int32", name="word_input")
x1 = Embedding(len(vocab), 100, input_length=mxlen, weights=[embeddings], trainable=False)(word_input)
x1 = LSTM(100)(x1)
y = Dense(6, activation="softmax", name="main_output")(x1)
model = Model(inputs=[word_input], outputs=[y])
adam = optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(optimizer=adam,loss='categorical_crossentropy',metrics=['categorical_accuracy']) # have to look into it
model.fit(x_train, y_train, epochs=30, batch_size=40, verbose=1)
x_train and y_train has the following shape - (10240, 198) and (10240,).
I am getting the following error:
Your output layer has 6 outputs (probably 6 classes are given), but the target labels in y_train are given as integers, apparently (array is flat). You need to convert y_train to a one-hot encoded array first, otherwise, the cross-entropy loss cannot be computed. Input and output tensors must always be compatible with the inputs and outputs of the model.
Given that there are 6 classes in y_train, encoded as: 0,1,2,3,4,5
you require a conversion:
0 -> 1.,0.,0.,0.,0.,0.
1 -> 0.,1.,0.,0.,0.,0.
etc.
Try it like this:
import tf.keras.backend as K
...
num_classes = 6
y_one_hot = K.one_hot( y_train, num_classes )
....
model.fit(x_train, y_one_hot, epochs=30, batch_size=40, verbose=1)