Cosine_similarity
Source:R/gen-namespace-docs.R, R/gen-namespace-examples.R, R/gen-namespace.R
torch_cosine_similarity.RdCosine_similarity
cosine_similarity(x1, x2, dim=1, eps=1e-8) -> Tensor
Returns cosine similarity between x1 and x2, computed along dim.
$$ \mbox{similarity} = \frac{x_1 \cdot x_2}{\max(\Vert x_1 \Vert _2 \cdot \Vert x_2 \Vert _2, \epsilon)} $$
Examples
if (torch_is_installed()) {
input1 = torch_randn(c(100, 128))
input2 = torch_randn(c(100, 128))
output = torch_cosine_similarity(input1, input2)
output
}
#> torch_tensor
#> 0.0065
#> 0.1449
#> 0.1412
#> 0.1311
#> -0.1727
#> 0.1064
#> 0.1792
#> 0.0446
#> -0.0201
#> -0.0629
#> 0.1670
#> 0.0301
#> -0.0829
#> -0.1224
#> 0.0256
#> 0.0651
#> -0.0286
#> 0.0125
#> 0.0793
#> -0.0248
#> 0.0050
#> 0.0419
#> -0.0857
#> 0.0677
#> -0.0726
#> -0.1083
#> 0.0006
#> -0.0792
#> 0.0490
#> -0.0757
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]