You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_cosine_similarity.py 3.1 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """test cosine_similarity"""
  16. import pytest
  17. import numpy as np
  18. from sklearn.metrics import pairwise
  19. from mindspore.nn.metrics import CosineSimilarity
  20. def test_cosine_similarity():
  21. """test_cosine_similarity"""
  22. test_data = np.array([[5, 8, 3, 2], [5, 8, 3, 2], [4, 2, 3, 4]])
  23. metric = CosineSimilarity()
  24. metric.clear()
  25. metric.update(test_data)
  26. square_matrix = metric.eval()
  27. assert np.allclose(square_matrix, np.array([[0, 1, 0.78229315], [1, 0, 0.78229315], [0.78229315, 0.78229315, 0]]))
  28. def test_cosine_similarity_compare():
  29. """test_cosine_similarity_compare"""
  30. test_data = np.array([[5, 8, 3, 2], [5, 8, 3, 2], [4, 2, 3, 4]])
  31. metric = CosineSimilarity(similarity='cosine', reduction='none', zero_diagonal=False)
  32. metric.clear()
  33. metric.update(test_data)
  34. ms_square_matrix = metric.eval()
  35. def sklearn_cosine_similarity(test_data, similarity, reduction):
  36. """sklearn_cosine_similarity"""
  37. metric_func = {'cosine': pairwise.cosine_similarity,
  38. 'dot': pairwise.linear_kernel}[similarity]
  39. square_matrix = metric_func(test_data, test_data)
  40. if reduction == 'mean':
  41. return square_matrix.mean(axis=-1)
  42. if reduction == 'sum':
  43. return square_matrix.sum(axis=-1)
  44. return square_matrix
  45. sk_square_matrix = sklearn_cosine_similarity(test_data, similarity='cosine', reduction='none')
  46. assert np.allclose(sk_square_matrix, ms_square_matrix)
  47. def test_cosine_similarity_init1():
  48. """test_cosine_similarity_init1"""
  49. with pytest.raises(ValueError):
  50. CosineSimilarity(similarity="4")
  51. def test_cosine_similarity_init2():
  52. """test_cosine_similarity_init2"""
  53. with pytest.raises(TypeError):
  54. CosineSimilarity(similarity=4)
  55. def test_cosine_similarity_init3():
  56. """test_cosine_similarity_init3"""
  57. with pytest.raises(TypeError):
  58. CosineSimilarity(reduction=2)
  59. def test_cosine_similarity_init4():
  60. """test_cosine_similarity_init4"""
  61. with pytest.raises(ValueError):
  62. CosineSimilarity(reduction="1")
  63. def test_cosine_similarity_init5():
  64. """test_cosine_similarity_init5"""
  65. with pytest.raises(TypeError):
  66. CosineSimilarity(zero_diagonal=3)
  67. def test_cosine_similarity_runtime():
  68. """test_cosine_similarity_runtime"""
  69. metric = CosineSimilarity()
  70. metric.clear()
  71. with pytest.raises(RuntimeError):
  72. metric.eval()