How to test a custom loss function in keras?
I think one way of achieving that is using a Keras backend function. Here we define a function that takes as input two tensors and returns as output a tensor:
from keras import Model
from keras import layers
x = layers.Input(shape=(None,))
y = layers.Input(shape=(None,))
loss_func = K.function([x, y], [masked_loss_function(x, y, 0)])
And now we can use loss_func
to run the computation graph we have defined:
assert loss_func([[[1,0]], [[1,1]]]) == [[0]]
Note that keras backend function, i.e. function
, expects that the input and output arguments be an array of tensors. Additionally, x
and y
takes a batch of tensors, i.e. an array of tensors, with undefined shape.
This is another workaround,
x = [1, 0]
y = [1, 1]
F = masked_loss_function(K.variable(x), K.variable(y), K.variable(0))
assert K.eval(F) == 0