如何解决使用Tensorflow的三重损失
我正在尝试使用张量流计算三重音损失,代码为:
# Step 1: Compute the (encoding) distance between the anchor and the positive
pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor,positive)))
# Step 2: Compute the (encoding) distance between the anchor and the negative
neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor,negative)))
# Step 3: subtract the two previous distances and add alpha.
basic_loss = tf.add(tf.subtract(pos_dist,neg_dist),alpha)
# Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
loss = tf.maximum(tf.reduce_sum(basic_loss),0)
但是我没有得到正确的答案。
当我随机访问以下代码时,它给了我正确的答案。
# Step 1: Compute the (encoding) distance between the anchor and the positive,you will need to sum over axis=-1
pos_dist = tf.reduce_sum(tf.square(anchor - positive),axis = -1)
# Step 2: Compute the (encoding) distance between the anchor and the negative,you will need to sum over axis=-1
neg_dist = tf.reduce_sum(tf.square(anchor - negative),axis = -1)
# Step 3: subtract the two previous distances and add alpha.
basic_loss = pos_dist - neg_dist + alpha
# Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
loss = tf.reduce_sum(tf.maximum(basic_loss,0.0))
如果有人能帮助我找出错误的地方,那就太好了。谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。