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.0106
#> 0.0462
#> 0.0225
#> -0.0280
#> -0.0758
#> 0.1148
#> 0.0002
#> 0.0741
#> 0.0000
#> -0.1320
#> 0.0299
#> -0.0354
#> -0.0308
#> -0.0399
#> 0.0015
#> -0.0119
#> 0.0876
#> -0.1525
#> 0.1750
#> 0.1743
#> 0.0715
#> 0.0495
#> -0.0582
#> 0.0520
#> -0.0923
#> -0.0001
#> -0.1454
#> 0.0377
#> -0.0451
#> -0.0971
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]