张量流中 pytorch 的 torch.nn.CosineEmbeddingLoss 相当于什么?

use*_*018 2 python cosine-similarity deep-learning tensorflow pytorch

Pytorch中的CosineEmbeddingLoss是我在tensorflow中寻找的完美函数,但我只能找到tf.losses.cosine_distance。有没有一种方法或代码可以在tensorflow中编写CosineEmbeddingLoss?

All*_*oie 6

TensorFlow 版本CosineEmbeddingLoss

import tensorflow as tf
from tensorflow import keras

cosine_similarity_loss = keras.losses.CosineSimilarity(
    reduction='none'
)

# target variable can be also passed along with margin
# it can be either target=1 or target = -1
# by this, CosineEmbeddingLoss methoc can be used 
# inside the `model.compile` with ease.
def CosineEmbeddingLoss(margin=0.):
    def cosine_embedding_loss_fn(input_one, input_two, target):
        similarity = - cosine_similarity_loss(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
Run Code Online (Sandbox Code Playgroud)

与 Torch 的版本一起运行:

import numpy as np 
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()

tf_result = CosineEmbeddingLoss(margin=0.5)(
    first_values, second_values, labels
).numpy()

print(torch_result, tf_result)
Run Code Online (Sandbox Code Playgroud)

似乎在合理的精度内匹配:

0.58433354 0.5843335801639801
Run Code Online (Sandbox Code Playgroud)