WibeMan
WibeMan

Reputation: 91

Keras: Unable to use custom loss function in my model

I'm building a language model using Keras and I would like to use perplexity as my loss function, However when I tried to compile my model with my loss function, I get a value error saying that the loss function is unknown.

My loss function looks as follows:

def perplexity_loss(y_true, y_pred):
    """
    The perplexity metric. Why isn't this part of Keras yet?!
    https://stackoverflow.com/questions/41881308/how-to-calculate-perplexity-of-rnn-in-tensorflow
    https://github.com/keras-team/keras/issues/8267
    """
    cross_entropy = keras.losses.SparseCategoricalCrossentropy(y_true, y_pred)
    perplexity = tf.keras.backend.exp(cross_entropy)
    return perplexity

And this is how I initiate my model:

# define model
model = Sequential()
model.add(Embedding(vocab_size, 500, input_length=max_length-1))
model.add(LSTM(750))
model.add(Dense(vocab_size, activation='softmax'))
# compile network
model.compile(loss='perplexity_loss', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(X, y, epochs=150, verbose=2)

I get the following error:

ValueError: Unknown loss function:perplexity

Upvotes: 2

Views: 1254

Answers (1)

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39790

The error is caused because instead of the function, you are passing a string ('perplexity_loss'). The following should do the trick:

model.compile(loss=perplexity_loss, optimizer='adam', metrics=['accuracy'])

Note that when you'd want to load the model again, you would have to use:

from keras.models import load_model

model = load_model('my_model.h5', custom_objects={'perplexity_loss': perplexity_loss})

Upvotes: 2

Related Questions