Pytorch中的CosineEmbeddingLoss是我在tensorflow中寻找的完美函数,但我只能找到tf.losses.cosine_distance。有没有一种方法或代码可以在tensorflow中写入CosineEmbeddingLoss?
答案 0 :(得分:2)
CosineEmbeddingLoss的TensorFlow版本:
import tensorflow as tf
def CosineEmbeddingLoss(margin=0.):
def _cosine_similarity(x1, x2):
"""Cosine similarity between two batches of vectors."""
return tf.reduce_sum(x1 * x2, axis=-1) / (
tf.norm(x1, axis=-1) * tf.norm(x2, axis=-1))
def _cosine_embedding_loss_fn(input_one, input_two, target):
similarity = _cosine_similarity(input_one, input_two)
return tf.reduce_mean(tf.where(
tf.equal(target, 1),
1. - similarity,
tf.maximum(tf.zeros_like(similarity), similarity - margin)))
return _cosine_embedding_loss_fn
将它与Torch的版本一起运行:
import tensorflow as tf
import numpy
import torch
from torch.autograd import Variable
first_values = numpy.random.normal(size=[100, 3])
second_values = numpy.random.normal(size=[100, 3])
labels = numpy.random.randint(2, size=[100]) * 2 - 1
torch_result = torch.nn.CosineEmbeddingLoss(margin=0.5)(
Variable(torch.FloatTensor(first_values)),
Variable(torch.FloatTensor(second_values)),
Variable(torch.IntTensor(labels))).data.numpy()
with tf.Graph().as_default():
with tf.Session():
tf_result = CosineEmbeddingLoss(margin=0.5)(
first_values, second_values, labels).eval()
print(torch_result, tf_result)
似乎在合理的精确度内匹配:
array([ 0.35702518], dtype=float32) 0.35702516587462357