triplet_loss.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:masalachai 作者: DaikiShimada 项目源码 文件源码
def forward(self, inputs):
        xp = cuda.get_array_module(*inputs)

        anchor, positive, negative = inputs
        N = anchor.shape[0]

        def euclidean_d(v1, v2):
            return chainer.functions.sum(chainer.functions.math.sqrt((v1-v2) ** 2), axis=1)
        d_ap = euclidean_d(anchor, positive)
        d_an = euclidean_d(anchor, negative)

        dist_p = chainer.functions.exp(d_ap)\
                / (chainer.functions.exp(d_ap)\
                + chainer.functions.exp(d_an))
        loss = chainer.functions.sum(dist_p * dist_p) / N
        return xp.array(loss, dtype=numpy.float32),
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号