Issue
That is the issue I am getting, but I don't use it as a bool anywhere. Or at least not that I can see. I have read similar questions, they were resolved because there was in fact an instance where it is being treated as a bool. However, I have not used it as a bool.
Functions:
def compile_cnn(model, loss = None, optimizer = None):
# Compile the CNN using the specified loss function
model.compile(loss=loss, optimizer=optimizer)
# return the compiled model
return model
Custom Keras Loss functions:
def contrastive_loss(label, embedding, margin = 0.4):
# Assign the label
y = label
# Assign the embeddings
p1 = embedding[0]
p2 = embedding[1]
# Get the euclean distance
d = tf.norm(p1 - p2, axis=-1)
if y == 0:
return (1/2) * math.sqrt(d)
else:
return (1/2) * math.sqrt(max(0, (margin-d)))
Calling Code:
# We use Adam as optimizer
optimizer = keras.optimizers.Adam()
# Compile the model with the contrastive loss function
cont_loss_model = compile_cnn(model, contrastive_loss, optimizer)
Here is the full error message as requested.
C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
C:\Users\User\Documents\Uni\2020\IFN680\Assignment 2\SiameseNetwork.py:88 contrastive_loss *
return (1/2) * tf.math.sqrt(max(0, (margin-d)))
C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:877 __bool__ **
self._disallow_bool_casting()
C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:486 _disallow_bool_casting
self._disallow_when_autograph_enabled(
C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:472 _disallow_when_autograph_enabled
raise errors.OperatorNotAllowedInGraphError(
OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
Solution
This was solved by adding @tf.function
to the custom loss function:
@tf.function
def contrastive_loss(label, embedding, margin = 0.4):
Answered By - Cornelis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.