Currently, I am trying to create a simple LSTM to generate new text data. I want to use BLEU metric for the model evaluation. Both BLEU metric and LSTM are from keras library. I want to use a BLEU metric during training. However, when I put the metric in the model.compile() method, later, the model.fit() method throws an error.
I read somewhere that the error has something to do with a function not being a tensor, but I don't know how true it is and don't know what function should be converted to a tensor.
Here is the LSTM implementation with BLEU metric:
bleu = Bleu(tokenizer=tokenizer, name='bleu')
model = Sequential([
Embedding(dictionary_length, max_len, input_length=max_len-1),
LSTM(150), # LSTM vrstva
Dense(dictionary_length, activation='softmax')
])
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=[bleu])
early_stop_callback = EarlyStopping(monitor='val_loss', patience=5) # callback na zastavenie treningu
checkpoint_callback = ModelCheckpoint(filepath='./drive/MyDrive/colab/3best_model_v2_uni_bleu.h5', monitor='val_loss', save_best_only=True) # callback na ulozenie najlepsieho modelu
history = model.fit(train_sequences, last_words, epochs=20, validation_split=0.2, batch_size=128, callbacks=[early_stop_callback, checkpoint_callback])
Here is the exact error that gets thrown when calling the method:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-20-1728b7eae8ff> in <cell line: 4>()
2 checkpoint_callback = ModelCheckpoint(filepath='./drive/MyDrive/colab/3best_model_v2_uni_bleu.h5', monitor='val_loss', save_best_only=True) # callback na ulozenie najlepsieho modelu
3
----> 4 history = model.fit(train_sequences, last_words, epochs=20, validation_split=0.2, batch_size=128, callbacks=[early_stop_callback, checkpoint_callback])
7 frames
/usr/local/lib/python3.9/dist-packages/keras_nlp/metrics/bleu.py in if_body_1()
84
85 def if_body_1():
---> 86 raise ag__.converted_call(ag__.ld(ValueError), (f'{ag__.ld(tensor_name)} is of rank {ag__.ld(input).shape.rank}. The last dimension must be of size 1.',), None, fscope_1)
87
88 def else_body_1():
AttributeError: in user code:
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1284, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.9/dist-packages/keras_nlp/metrics/bleu.py", line 328, in validate_and_fix_rank *
raise ValueError(
AttributeError: 'function' object has no attribute 'shape'