Skip to main content
 首页 » 编程设计

python之什么相当于 pytorch 在 tensorflow 中的 torch.nn.CosineEmbeddingLoss

2024年10月01日31lonelyxmas

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

请您参考如下方法:

CosineEmbeddingLoss 的 TensorFlow 版本:

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 

与 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) 

似乎匹配到合理的精度:

0.58433354 0.5843335801639801