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.1293
#> -0.0003
#> -0.0180
#> -0.0255
#> 0.1714
#> -0.0355
#> 0.0173
#> -0.0141
#> 0.0084
#> 0.1753
#> 0.0137
#> -0.1437
#> -0.0147
#> -0.0553
#> 0.0042
#> -0.1465
#> -0.1073
#> 0.0491
#> 0.0676
#> -0.1218
#> 0.0472
#> -0.1519
#> 0.1204
#> 0.0003
#> 0.0402
#> -0.1354
#> 0.1057
#> -0.0168
#> 0.0323
#> 0.0405
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]