Skip to contents

Cosine_similarity

Usage

torch_cosine_similarity(x1, x2, dim = 2L, eps = 1e-08)

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.

similarity=x1x2max(x12x22,ϵ)

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.1676
#>  0.0988
#> -0.1302
#> -0.1581
#> -0.0654
#>  0.0243
#>  0.0110
#> -0.0974
#>  0.0993
#> -0.0466
#>  0.0571
#> -0.0185
#>  0.0312
#>  0.0125
#> -0.0400
#>  0.1636
#> -0.1599
#> -0.0683
#> -0.0341
#>  0.0006
#> -0.0665
#>  0.0469
#>  0.0857
#> -0.0465
#>  0.0015
#>  0.0369
#>  0.0041
#>  0.1600
#>  0.1003
#>  0.0976
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{100} ]