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.0696
#> 0.1017
#> 0.0997
#> 0.0271
#> -0.0702
#> 0.1115
#> 0.0374
#> 0.0652
#> 0.0067
#> 0.2535
#> 0.1018
#> 0.0276
#> -0.0774
#> 0.0946
#> -0.0288
#> -0.1134
#> 0.0072
#> 0.1359
#> 0.0328
#> 0.0058
#> 0.0152
#> 0.0777
#> 0.0776
#> 0.0509
#> 0.0454
#> -0.0583
#> 0.1607
#> -0.0631
#> 0.0201
#> 0.0493
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]