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.0518
#> -0.1353
#> 0.0055
#> 0.1350
#> 0.0106
#> 0.0236
#> 0.0152
#> -0.0359
#> -0.0339
#> 0.0344
#> 0.0024
#> 0.0932
#> 0.0735
#> 0.0522
#> 0.0258
#> 0.0779
#> -0.1295
#> -0.0928
#> 0.0746
#> 0.0432
#> 0.0355
#> 0.0251
#> 0.0303
#> -0.0159
#> 0.1192
#> 0.0507
#> 0.1229
#> -0.0884
#> -0.0054
#> -0.0657
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]