I am trying to code a neural network which has two loss functions: standard mse and a customized loss. The second takes in input the output of an intermediate layer of the network and includes an if-statement. This second loss function, which I add to the model through add_loss is the following:
def intermediate_out(middle_layer):
x_coord = K.reshape(middle_layer[:,1:72], shape=(-1,71))
# Some operations on x_coord
# print(x_coord)
if K.any(K.less(x_coord[:,1:]-x_coord[:,:-1],0)):
return K.constant(100)
else:
return K.constant(0)
Since this code generated errors, I tried to replace the return with the following lines:
return K.switch(K.any(K.less(x_coord[:,1:]-x_coord[:,:-1],0)),
K.constant(100),
K.constant(0))
and
return tf.cond(K.any(K.less(x_coord[:,1:]-x_coord[:,:-1],0)),
lambda: 100, lambda: 0)
But the error I get is the usual: OperatorNotAllowedInGraphError: using a tf.Tensor as a Python bool is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
But if I try to add the decorator @tf.function to the loss function intermediate_out, then I get the error: _SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'batpj_layer/Identity:0' shape=(None, 143) dtype=float32>]
This is how I am calling add_loss:
model.add_loss(intermediate_out(out)) # out is the intermediate layer
model.compile(optimizer=method, loss='mse') # method is 'Nadam'
Do you have any suggestions/ideas? Thank you in advance. I am using Tensorflow 2.1.0.
Aucun commentaire:
Enregistrer un commentaire