jeudi 14 juin 2018

How to test a custom loss function in keras?

I am training a CovNet with two outputs. My training samples look like this:

[0, value_a1], [0, value_a2], ...

and

[value_b1, 0], [value_b2, 0], ....

I want to generate my own loss function and mask pairs that contain the mask_value = 0. I have this function, though I am not sure whether it really does what I want. So, I want to write some tests.

from tensorflow.python.keras import backend as K
from tensorflow.python.keras import losses

def masked_loss_function(y_true, y_pred, mask_value=0):
    '''
    This model has two target values which are independent of each other.
    We mask the output so that only the value that is used for training 
    contributes to the loss.
        mask_value : is the value that is not used for training
    '''
    mask = K.cast(K.not_equal(y_true, mask_value), K.floatx())
    return losses.mean_squared_error(y_true * mask, y_pred * mask)

Though, I don't know how I can test this function with keras? Usually, this would be passed to model.compile(). Something like along these lines:

x = [1, 0]
y = [1, 1]
assert masked_loss_function(x, y, 0) == 0

Aucun commentaire:

Enregistrer un commentaire