Cosine_similarity
Source:R/gen-namespace-docs.R
, R/gen-namespace-examples.R
, R/gen-namespace.R
torch_cosine_similarity.Rd
Cosine_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.0466
#> -0.1372
#> -0.0302
#> 0.0493
#> 0.0217
#> 0.0185
#> 0.1085
#> -0.1180
#> -0.0297
#> 0.0583
#> -0.0547
#> 0.0357
#> 0.0243
#> 0.0327
#> 0.0973
#> -0.0581
#> -0.1382
#> 0.0273
#> 0.1538
#> -0.0739
#> -0.0360
#> 0.0212
#> -0.0070
#> -0.0633
#> -0.0089
#> -0.0807
#> -0.0778
#> -0.0124
#> 0.1215
#> -0.1192
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]