mindspore.nn.CosineSimilarity
- class mindspore.nn.CosineSimilarity(similarity='cosine', reduction='none', zero_diagonal=True)[source]
Computes representation similarity
- Parameters
- Returns
A square matrix (input1, input1) with the similarity scores between all elements. If sum or mean are used, then returns (b, 1) with the reduced value for each row
Example
>>> 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.]]