【问题标题】:Torch Lua, how to compute the cosine distance for each pair of single values of two tensors?Torch Lua,如何计算两个张量的每对单值的余弦距离?
【发布时间】:2016-08-10 15:01:17
【问题描述】:

我在 Torch 中遇到了当前的编程问题。

我有一张由两个张量组成的表格:

require 'nn'
N = 4
aaaTensor = torch.randn(N)
bbbTensor = torch.randn(N)
thisTable = {aaaTensor, bbbTensor}

我想计算 aaaTensor 和 bbbTensor 的每对单个值的 余弦距离

  • aaaTensor[1]bbbTensor[1] 之间的余弦距离
  • aaaTensor[2]bbbTensor[2] 之间的余弦距离
  • ...
  • aaaTensor[N]bbbTensor[N] 之间的余弦距离

我不知道该怎么做。 如果我使用 nn.CosineDistance() 模块 (link),它将计算 aaaTensor 和 bbbTensor 之间的一般余弦距离

cosine = nn.CosineDistance()
cosine:forward{aaaTensor, bbbTensor}
0.7185
[torch.DoubleTensor of size 1]

我想要 N=4 个输出。

如何实现这种一对一的余弦距离计算? 谢谢

【问题讨论】:

  • 通常我希望余弦距离应用于两个向量,但 aaaTensor[1]bbbTensor[2] 只是数字。两个数字的余弦差应该是什么意思?

标签: lua torch cosine-similarity trigonometry


【解决方案1】:

documentation 表示 nn.CosineDistance() 接受批次。所以(虽然单个值的余弦距离没有意义)你可以这样做;

require 'nn'
N = 4
aaaTensor = torch.randn(N,1)
bbbTensor = torch.randn(N,1)
thisTable = {aaaTensor, bbbTensor}
cosine = nn.CosineDistance()
cosine:forward{aaaTensor, bbbTensor}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 2020-06-18
    • 1970-01-01
    • 2017-09-07
    • 2017-09-15
    • 1970-01-01
    • 2021-07-15
    • 2017-09-06
    相关资源
    最近更新 更多