首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >三重损失tensorflow,其中锚-正对是预先定义的

三重损失tensorflow,其中锚-正对是预先定义的
EN

Stack Overflow用户
提问于 2019-04-19 01:56:14
回答 1查看 151关注 0票数 0

我正在尝试在Tensorflow中实现三元组损失,其中三元组是通过在线挖掘的方式获得的。在我的特殊问题中,我已经有了anchor(image) - positive(text)对。我想要实现的是在批处理中拥有image-text对的三元组anchor(image) - positive(text) - negative(text)anchor(text) - positive(image) - negative(image)

如果您需要任何进一步的信息,请让我知道,并期待您的答案!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-19 04:49:19

我发现这就是我需要的解决方案:

代码语言:javascript
复制
def compute_loss(images: tf.Tensor, texts: tf.Tensor, margin: float) -> tf.Tensor:
    with tf.variable_scope(name_or_scope="loss"):
        scores = tf.matmul(images, texts, transpose_b=True)
        diagonal = tf.diag_part(scores)
        # Compare every diagonal score to scores in its column i.e
        # All contrastive images for each sentence
        cost_s = tf.maximum(0.0, margin - tf.reshape(diagonal, [-1, 1]) + scores)
        # Compare every diagonal score to scores in its row i.e
        # All contrastive sentences for each image
        cost_im = tf.maximum(0.0, margin - diagonal + scores)

        # Clear diagonals
        cost_s = tf.linalg.set_diag(cost_s, tf.zeros(tf.shape(cost_s)[0]))
        cost_im = tf.linalg.set_diag(cost_im, tf.zeros(tf.shape(cost_im)[0]))

        # For each positive pair (i,s) sum over the negative images
        cost_s = tf.reduce_sum(cost_s, axis=1)
        # For each positive pair (i,s) sum over the negative texts
        cost_im = tf.reduce_sum(cost_im, axis=0)

        triplet_loss = tf.reduce_mean(cost_s) + tf.reduce_mean(cost_im)

        return triplet_loss
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55751431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档