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),
评论列表
文章目录