mindspore.ops.cosine_similarity

View Source On Gitee
mindspore.ops.cosine_similarity(x1, x2, dim=1, eps=1e-08)[source]

Calculate cosine similarity between two input tensors along the specified dimension.

similarity=x1x2max(x12x22,ϵ)

Note

Currently, broadcast of input is not supported.

Parameters
  • x1 (Tensor) – The first input tensor.

  • x2 (Tensor) – The second input tensor.

  • dim (int, optional) – Specify the dimension for computation. Default 1 .

  • eps (float, optional) – Minimal value to avoid division by zero. Default 1e-08 .

Returns

Tensor

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> input = mindspore.tensor([[-0.0256, 0.0127, -0.2475, 0.2316, 0.8037],
...                           [0.5809, -1.2712, -0.7038, -0.2558, 0.7494]])
>>> other = mindspore.tensor([[-0.6115, -0.1965, -0.8484, 0.2389, 0.2409],
...                           [1.8940, -2.1997, 0.1915, 0.0856, 0.7542]])
>>> output = mindspore.ops.cosine_similarity(input, other)
>>> print(output)
[0.4843164  0.81647635]