Cosine_similarity
Source:R/gen-namespace-docs.R
, R/gen-namespace-examples.R
, R/gen-namespace.R
torch_cosine_similarity.Rd
Cosine_similarity
Arguments
- x1
(Tensor) First input.
- x2
(Tensor) Second input (of size matching x1).
- dim
(int, optional) Dimension of vectors. Default: 1
- eps
(float, optional) Small value to avoid division by zero. Default: 1e-8
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.0949
#> 0.0194
#> -0.1097
#> 0.0712
#> -0.0409
#> 0.0385
#> 0.1187
#> 0.0047
#> -0.0886
#> -0.0146
#> 0.1420
#> 0.1234
#> 0.0955
#> -0.0307
#> -0.2110
#> -0.0898
#> -0.1660
#> 0.0222
#> -0.1334
#> 0.0068
#> -0.0689
#> 0.0312
#> 0.1573
#> -0.0273
#> -0.1179
#> -0.0114
#> -0.0153
#> 0.0173
#> 0.0982
#> 0.0696
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]