简体   繁体   English

keras model with tf.contrib.losses.metric_learning.triplet_semihard_loss断言错误

[英]keras model with tf.contrib.losses.metric_learning.triplet_semihard_loss Assertion error

I am using python 3 with anaconda, and trying to use a tf.contrib loss function with a Keras model. 我正在使用带有anaconda的python 3,并尝试使用带有Keras模型的tf.contrib损失函数。

The code is the following 代码如下

from keras.layers import Dense, Flatten
from keras.optimizers import Adam
from keras.models import Sequential
from tensorflow.contrib.losses import metric_learning
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(50,  activation="relu"))
model.compile(loss=metric_learning.triplet_semihard_loss, optimizer=Adam())

I get the following error: 我收到以下错误:

File "/home/user/.local/lib/python3.6/site-packages/keras/engine/training_utils.py", line 404, in weighted score_array = fn(y_true, y_pred) File "/home/user/anaconda3/envs/siamese/lib/python3.6/site-packages/tensorflow/contrib/losses/python/metric_learning/metric_loss_ops.py", line 179, in triplet_semihard_loss assert lshape.shape == 1 AssertionError 文件“/home/user/.local/lib/python3.6/site-packages/keras/engine/training_utils.py”,第404行,加权score_array = fn(y_true,y_pred)文件“/ home / user / anaconda3 /envs/siamese/lib/python3.6/site-packages/tensorflow/contrib/losses/python/metric_learning/metric_loss_ops.py“,第179行,在triplet_semihard_loss断言lshape.shape == 1 AssertionError

When I am using the same network with a keras loss function it works fine, I tried to wrap the tf loss function in a function like so 当我使用具有keras损失功能的相同网络时它工作正常,我试图将tf loss函数包装在一个像这样的函数中

def func(y_true, y_pred): 
    import tensorflow as tf
    return tf.contrib.losses.metric_learning.triplet_semihard_loss(y_true, y_pred) 

And still getting the same error 仍然得到同样的错误

What am I doing wrong here? 我在这做错了什么?

update: When changing the func to return the following 更新:更改func时返回以下内容

return K.categorical_crossentropy(y_true, y_pred)

everything works fine! 一切正常! But i cant get it to work with the specific tf loss function... 但是我无法使用特定的tf损失函数...

When i go into tf.contrib.losses.metric_learning.triplet_semihard_loss and remove this line of code: assert lshape.shape == 1 it runs fine 当我进入tf.contrib.losses.metric_learning.triplet_semihard_loss并删除这行代码时: assert lshape.shape == 1它运行正常

Thanks 谢谢

The problem is that you pass wrong input to the loss function. 问题是您将错误的输入传递给损失函数。

According to triplet_semihard_loss docstring you need to pass labels and embeddings . 根据triplet_semihard_loss docstring,您需要传递labelsembeddings

So your code have to be: 所以你的代码必须是:

def func(y, embeddings): 
    return tf.contrib.losses.metric_learning.triplet_semihard_loss(labels=y, embeddings=embeddings) 

And two more notes about network for embeddings: 还有两个关于嵌入网络的注释:

  1. Last dense layer has to be without activation 最后的密集层必须没有激活

  2. Don't forget to normalise output vector model.add(Lambda(lambda x: K.l2_normalize(x, axis=1))) 不要忘记规范化输出矢量model.add(Lambda(lambda x: K.l2_normalize(x, axis=1)))

It seems that your problem comes from an incorrect input in the loss function. 看来您的问题来自丢失函数中的错误输入。 In fact, the triplet loss wants the parameters: 事实上,三重态损失需要参数:

Args:
labels: 1-D tf.int32 `Tensor` with shape [batch_size] of
  multiclass integer labels.
embeddings: 2-D float `Tensor` of embedding vectors. Embeddings should
  be l2 normalized.

Are you sure that y_true has the correct shape? 你确定y_true有正确的形状吗? Can you give us more details about the tensors you are using? 您能否提供有关您正在使用的张量的更多详细信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用“tfa.losses.triplet_semihard_loss”? - How does "tfa.losses.triplet_semihard_loss" get called? 张量流图正则化 (NSL) 如何影响三重半硬损失 (TFA) - How tensorflow graph regularization (NSL) affects triplet semihard loss (TFA) tensorflow Triplet_semihard_loss 在多个时期后不会改变 - tensorflow triplet_semihard_loss doesnt change after multiple epochs 如何在 Tensorflow2.x 中按子类 tf.keras.losses.Loss class 自定义损失 - How to custom losses by subclass tf.keras.losses.Loss class in Tensorflow2.x 根据三重态损耗正确向Keras模型提交3个输入 - Correctly submitting 3 inputs to a Keras model based on Triplet Loss 访问损耗指标来自 keras model - Access loss metric from keras model 在 keras 中实现三元组损失的准确性 - Implementing accuracy for triplet loss in keras tf.Keras 中自定义损失和自定义指标函数中的目标 - Targets in custom loss and custom metric function in tf.Keras tf.keras.losses.SparseCategoricalCrossentropy() 与“sparse_categorical_crossentropy”作为损失的区别 - difference between tf.keras.losses.SparseCategoricalCrossentropy() vs “sparse_categorical_crossentropy” as loss 将tf.contrib.opt.ScipyOptimizerInterface与tf.keras.layers一起使用,损失不会改变 - Using tf.contrib.opt.ScipyOptimizerInterface with tf.keras.layers, loss not changing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM