mindspore.train.CosineSimilarity

class mindspore.train.CosineSimilarity(similarity='cosine', reduction='none', zero_diagonal=True)[source]

Computes representation similarity.

Parameters
  • similarity (str) – the computation logit. 'cosine' means computing similarity. 'dot' means computing dots of arrays, Default: 'cosine' .

  • reduction (str) – Specifies the reduction to be applied to the output. Support 'none', 'sum', 'mean' (all along dim -1). Default: 'none' .

  • zero_diagonal (bool) – If True, diagonals of results will be set to zero. Default: True .

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore.train import CosineSimilarity
>>>
>>> test_data = np.array([[1, 3, 4, 7], [2, 4, 2, 5], [3, 1, 5, 8]])
>>> metric = CosineSimilarity()
>>> metric.clear()
>>> metric.update(test_data)
>>> square_matrix = metric.eval()
>>> print(square_matrix)
[[0.  0.94025615  0.95162452]
 [0.94025615  0.  0.86146098]
 [0.95162452  0.86146098  0.]]
clear()[source]

Clears the internal evaluation result.

eval()[source]

Computes the similarity matrix.

Returns

numpy.ndarray. The similarity matrix.

Raises

RuntimeError – If the update method is not called first, an error will be reported.

update(inputs)[source]

Updates the internal evaluation result with ‘inputs’.

Parameters

inputs (Union[Tensor, list, numpy.ndarray]) – The input matrix including y and y_pred.