sojourner92
sojourner92

Reputation: 43

Custom metric in Keras using keras.losses.CategoricalCrossentropy

I'm struggling to implement a custom metric in Keras (2.4.3 with the tensorflow backend) such that I can trigger an early stopping mechanic. Essentially, I want to have Keras stop training a model should there be too big a decrease in the training loss function. To do this, I am using the following code:

def custom_metric(y_true,y_pred):
    y=keras.losses.CategoricalCrossentropy(y_true,y_pred)
    z=1.0/(1.0-y.numpy())
    return z

model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['categorical_accuracy',custom_metric])

custom_stop = EarlyStopping(monitor='custom_metric',min_delta=0,patience=2,
                            verbose=1,mode='min',restore_best_weights=True)

I'm getting errors along the lines of AttributeError: 'CategoricalCrossentropy' object has no attribute 'numpy', which I understand is due to the definition of z, but I can't get something equivalent to work using by replacing the floats in the definition of z with tf.constants or anything like that. Does anyone have any suggestions? Thanks a lot

Upvotes: 0

Views: 191

Answers (2)

Andrey
Andrey

Reputation: 6377

This should work:

def custom_metric(y_true,y_pred):
    y=keras.losses.categorical_crossentropy(y_true,y_pred)
    z=1.0/(1.0-y)
    return z

Upvotes: 1

Nicolas Gervais
Nicolas Gervais

Reputation: 36714

Use this instead, mind the spelling:

keras.losses.categorical_crossentropy(y_true,y_pred)

Upvotes: 1

Related Questions