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.0694
#> 0.1258
#> 0.0879
#> 0.1043
#> 0.0454
#> -0.1570
#> -0.0335
#> -0.0908
#> 0.0051
#> 0.0326
#> 0.1504
#> -0.1074
#> 0.1100
#> 0.0074
#> 0.1805
#> -0.1196
#> 0.0321
#> 0.1185
#> 0.1106
#> 0.1133
#> -0.1167
#> -0.0193
#> -0.0248
#> -0.0850
#> -0.1618
#> 0.0066
#> 0.0806
#> -0.0096
#> 0.1869
#> 0.1189
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]