这就是我遇到的问题,但是我在任何地方都不会将其当作笨蛋。或至少不在我能找到的地方。我已经读过类似的问题,所以已经解决了这些问题,因为实际上有一个实例将其视为布尔。但是,我并没有使用它。
功能:
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
自定义Keras损失函数:
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)))
呼叫代码:
# 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)
这是请求的完整错误消息。
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.
enter code here
在此处输入代码