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?