1
votes

Is it possible to use a Keras layer (pre-trained or fixed layer with no trainable parameters) inside a custom loss function?

I would like to do something like:

def custom_loss(y_true, y_pred):
    y_true_trans = SomeKerasLayer()(y_true)
    y_true_trans = SomeKerasLayer()(y_pred)
    return K.mean(K.abs(y_pred_trans - y_true_trans), axis=-1)

In the Tensorflow backend, I get the error:

File "/home/drb/venvs/keras/lib/python3.5/site-packages/tensorflow/python /framework/tensor_util.py", line 364, in make_tensor_proto

raise ValueError("None values not supported.")

ValueError: None values not supported.

Of course I could transform y_pred with the Keras layer outside the loss function (by providing an extra output), but I can't do the same with the reference value y_true.

Another way to rephrase the same question in more general terms would be: Is it possible to encapsulate a Keras layer as a Keras backend function?

Is there any solution or workaround?

1
which layer do you want to use? - Marcin Możejko
A custom layer which has no learnable parameters. - Daniel Arteaga
Can you show the definition? - Marcin Możejko
Unfortunately not, because it is not open source and I do not own the code. It is a moderately complex layer which does some FFT transformations inside. I know I could transplant the inner layer processing into the loss function by using the Keras backend, but this would amount to a lot of work and rewritting. I would like to avoid this if possible. - Daniel Arteaga
I actually noticed that it is possible to rephrase the question into a more general form: is it possible to encapsulate a fixed Keras layer as a Keras backend function? I will update the question accordingly. - Daniel Arteaga

1 Answers

0
votes

The question is kind of vague, so it has both a yes and no response.
Depending on your implementation you may try
model = keras.layers.Add(..something..)(x)
where x = the name of the previous relevant value.