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_vectors.py 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. import numpy as np
  16. import pytest
  17. from mindspore import log
  18. import mindspore.dataset as ds
  19. import mindspore.dataset.text as text
  20. import mindspore.dataset.text.transforms as T
  21. DATASET_ROOT_PATH = "../data/dataset/testVectors/"
  22. def test_vectors_all_tovectors_params_eager():
  23. """
  24. Feature: Vectors
  25. Description: test with all parameters which include `unk_init`
  26. and `lower_case_backup` in function ToVectors in eager mode
  27. Expectation: output is equal to the expected value
  28. """
  29. vectors = text.Vectors.from_file(DATASET_ROOT_PATH + "vectors.txt", max_vectors=4)
  30. myUnk = [-1, -1, -1, -1, -1, -1]
  31. to_vectors = T.ToVectors(vectors, unk_init=myUnk, lower_case_backup=True)
  32. result1 = to_vectors("Ok")
  33. result2 = to_vectors("!")
  34. result3 = to_vectors("This")
  35. result4 = to_vectors("is")
  36. result5 = to_vectors("my")
  37. result6 = to_vectors("home")
  38. result7 = to_vectors("none")
  39. res = [[0.418, 0.24968, -0.41242, 0.1217, 0.34527, -0.04445718411],
  40. [0.013441, 0.23682, -0.16899, 0.40951, 0.63812, 0.47709],
  41. [0.15164, 0.30177, -0.16763, 0.17684, 0.31719, 0.33973],
  42. [0.70853, 0.57088, -0.4716, 0.18048, 0.54449, 0.72603],
  43. [-1, -1, -1, -1, -1, -1],
  44. [-1, -1, -1, -1, -1, -1],
  45. [-1, -1, -1, -1, -1, -1]]
  46. res_array = np.array(res, dtype=np.float32)
  47. assert np.array_equal(result1, res_array[0])
  48. assert np.array_equal(result2, res_array[1])
  49. assert np.array_equal(result3, res_array[2])
  50. assert np.array_equal(result4, res_array[3])
  51. assert np.array_equal(result5, res_array[4])
  52. assert np.array_equal(result6, res_array[5])
  53. assert np.array_equal(result7, res_array[6])
  54. def test_vectors_from_file():
  55. """
  56. Feature: Vectors
  57. Description: test with only default parameter
  58. Expectation: output is equal to the expected value
  59. """
  60. vectors = text.Vectors.from_file(DATASET_ROOT_PATH + "vectors.txt")
  61. to_vectors = text.ToVectors(vectors)
  62. data = ds.TextFileDataset(DATASET_ROOT_PATH + "words.txt", shuffle=False)
  63. data = data.map(operations=to_vectors, input_columns=["text"])
  64. ind = 0
  65. res = [[0.418, 0.24968, -0.41242, 0.1217, 0.34527, -0.04445718411],
  66. [0, 0, 0, 0, 0, 0],
  67. [0.15164, 0.30177, -0.16763, 0.17684, 0.31719, 0.33973],
  68. [0.70853, 0.57088, -0.4716, 0.18048, 0.54449, 0.72603],
  69. [0.68047, -0.039263, 0.30186, -0.17792, 0.42962, 0.032246],
  70. [0.26818, 0.14346, -0.27877, 0.016257, 0.11384, 0.69923],
  71. [0, 0, 0, 0, 0, 0]]
  72. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  73. res_array = np.array(res[ind], dtype=np.float32)
  74. assert np.array_equal(res_array, d["text"]), ind
  75. ind += 1
  76. def test_vectors_from_file_all_buildfromfile_params():
  77. """
  78. Feature: Vectors
  79. Description: test with all parameters which include `path` and `max_vector` in function BuildFromFile
  80. Expectation: output is equal to the expected value
  81. """
  82. vectors = text.Vectors.from_file(DATASET_ROOT_PATH + "vectors.txt", max_vectors=100)
  83. to_vectors = text.ToVectors(vectors)
  84. data = ds.TextFileDataset(DATASET_ROOT_PATH + "words.txt", shuffle=False)
  85. data = data.map(operations=to_vectors, input_columns=["text"])
  86. ind = 0
  87. res = [[0.418, 0.24968, -0.41242, 0.1217, 0.34527, -0.04445718411],
  88. [0, 0, 0, 0, 0, 0],
  89. [0.15164, 0.30177, -0.16763, 0.17684, 0.31719, 0.33973],
  90. [0.70853, 0.57088, -0.4716, 0.18048, 0.54449, 0.72603],
  91. [0.68047, -0.039263, 0.30186, -0.17792, 0.42962, 0.032246],
  92. [0.26818, 0.14346, -0.27877, 0.016257, 0.11384, 0.69923],
  93. [0, 0, 0, 0, 0, 0]]
  94. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  95. res_array = np.array(res[ind], dtype=np.float32)
  96. assert np.array_equal(res_array, d["text"]), ind
  97. ind += 1
  98. def test_vectors_from_file_all_buildfromfile_params_eager():
  99. """
  100. Feature: Vectors
  101. Description: test with all parameters which include `path` and `max_vector` in function BuildFromFile in eager mode
  102. Expectation: output is equal to the expected value
  103. """
  104. vectors = text.Vectors.from_file(DATASET_ROOT_PATH + "vectors.txt", max_vectors=4)
  105. to_vectors = T.ToVectors(vectors)
  106. result1 = to_vectors("ok")
  107. result2 = to_vectors("!")
  108. result3 = to_vectors("this")
  109. result4 = to_vectors("is")
  110. result5 = to_vectors("my")
  111. result6 = to_vectors("home")
  112. result7 = to_vectors("none")
  113. res = [[0.418, 0.24968, -0.41242, 0.1217, 0.34527, -0.04445718411],
  114. [0.013441, 0.23682, -0.16899, 0.40951, 0.63812, 0.47709],
  115. [0.15164, 0.30177, -0.16763, 0.17684, 0.31719, 0.33973],
  116. [0.70853, 0.57088, -0.4716, 0.18048, 0.54449, 0.72603],
  117. [0, 0, 0, 0, 0, 0],
  118. [0, 0, 0, 0, 0, 0],
  119. [0, 0, 0, 0, 0, 0]]
  120. res_array = np.array(res, dtype=np.float32)
  121. assert np.array_equal(result1, res_array[0])
  122. assert np.array_equal(result2, res_array[1])
  123. assert np.array_equal(result3, res_array[2])
  124. assert np.array_equal(result4, res_array[3])
  125. assert np.array_equal(result5, res_array[4])
  126. assert np.array_equal(result6, res_array[5])
  127. assert np.array_equal(result7, res_array[6])
  128. def test_vectors_from_file_eager():
  129. """
  130. Feature: Vectors
  131. Description: test with only default parameter in eager mode
  132. Expectation: output is equal to the expected value
  133. """
  134. vectors = text.Vectors.from_file(DATASET_ROOT_PATH + "vectors.txt")
  135. to_vectors = T.ToVectors(vectors)
  136. result1 = to_vectors("ok")
  137. result2 = to_vectors("!")
  138. result3 = to_vectors("this")
  139. result4 = to_vectors("is")
  140. result5 = to_vectors("my")
  141. result6 = to_vectors("home")
  142. result7 = to_vectors("none")
  143. res = [[0.418, 0.24968, -0.41242, 0.1217, 0.34527, -0.04445718411],
  144. [0.013441, 0.23682, -0.16899, 0.40951, 0.63812, 0.47709],
  145. [0.15164, 0.30177, -0.16763, 0.17684, 0.31719, 0.33973],
  146. [0.70853, 0.57088, -0.4716, 0.18048, 0.54449, 0.72603],
  147. [0.68047, -0.039263, 0.30186, -0.17792, 0.42962, 0.032246],
  148. [0.26818, 0.14346, -0.27877, 0.016257, 0.11384, 0.69923],
  149. [0, 0, 0, 0, 0, 0]]
  150. res_array = np.array(res, dtype=np.float32)
  151. assert np.array_equal(result1, res_array[0])
  152. assert np.array_equal(result2, res_array[1])
  153. assert np.array_equal(result3, res_array[2])
  154. assert np.array_equal(result4, res_array[3])
  155. assert np.array_equal(result5, res_array[4])
  156. assert np.array_equal(result6, res_array[5])
  157. assert np.array_equal(result7, res_array[6])
  158. def test_vectors_invalid_input():
  159. """
  160. Feature: Vectors
  161. Description: test the validate function with invalid parameters.
  162. Expectation:
  163. """
  164. def test_invalid_input(test_name, file_path, error, error_msg, max_vectors=None,
  165. unk_init=None, lower_case_backup=False, token="ok"):
  166. log.info("Test Vectors with wrong input: {0}".format(test_name))
  167. with pytest.raises(error) as error_info:
  168. vectors = text.Vectors.from_file(file_path, max_vectors=max_vectors)
  169. to_vectors = T.ToVectors(vectors, unk_init=unk_init, lower_case_backup=lower_case_backup)
  170. to_vectors(token)
  171. assert error_msg in str(error_info.value)
  172. test_invalid_input("Not all vectors have the same number of dimensions",
  173. DATASET_ROOT_PATH + "vectors_dim_different.txt", error=RuntimeError,
  174. error_msg="all vectors must have the same number of dimensions, but got dim 5 while expecting 6")
  175. test_invalid_input("the file is empty.", DATASET_ROOT_PATH + "vectors_empty.txt",
  176. error=RuntimeError, error_msg="invalid file, file is empty.")
  177. test_invalid_input("the count of `unknown_init`'s element is different with word vector.",
  178. DATASET_ROOT_PATH + "vectors.txt",
  179. error=RuntimeError, error_msg="Unexpected error. ToVectors: " +
  180. "unk_init must be the same length as vectors, but got unk_init: 2 and vectors: 6",
  181. unk_init=[-1, -1])
  182. test_invalid_input("The file not exist", DATASET_ROOT_PATH + "not_exist.txt", error=RuntimeError,
  183. error_msg="get real path failed")
  184. test_invalid_input("The token is 1-dimensional",
  185. DATASET_ROOT_PATH + "vectors_with_wrong_info.txt", error=RuntimeError,
  186. error_msg="token with 1-dimensional vector.")
  187. test_invalid_input("max_vectors parameter must be greater than 0",
  188. DATASET_ROOT_PATH + "vectors.txt", error=ValueError,
  189. error_msg="Input max_vectors is not within the required interval", max_vectors=-1)
  190. test_invalid_input("invalid max_vectors parameter type as a float",
  191. DATASET_ROOT_PATH + "vectors.txt", error=TypeError,
  192. error_msg="Argument max_vectors with value 1.0 is not of type [<class 'int'>],"
  193. " but got <class 'float'>.", max_vectors=1.0)
  194. test_invalid_input("invalid max_vectors parameter type as a string",
  195. DATASET_ROOT_PATH + "vectors.txt", error=TypeError,
  196. error_msg="Argument max_vectors with value 1 is not of type [<class 'int'>],"
  197. " but got <class 'str'>.", max_vectors="1")
  198. test_invalid_input("invalid token parameter type as a float", DATASET_ROOT_PATH + "vectors.txt", error=RuntimeError,
  199. error_msg="input tensor type should be string.", token=1.0)
  200. test_invalid_input("invalid lower_case_backup parameter type as a string", DATASET_ROOT_PATH + "vectors.txt",
  201. error=TypeError, error_msg="Argument lower_case_backup with " +
  202. "value True is not of type [<class 'bool'>],"
  203. " but got <class 'str'>.", lower_case_backup="True")
  204. test_invalid_input("invalid lower_case_backup parameter type as a string", DATASET_ROOT_PATH + "vectors.txt",
  205. error=TypeError, error_msg="Argument lower_case_backup with " +
  206. "value True is not of type [<class 'bool'>],"
  207. " but got <class 'str'>.", lower_case_backup="True")
  208. if __name__ == '__main__':
  209. test_vectors_all_tovectors_params_eager()
  210. test_vectors_from_file()
  211. test_vectors_from_file_all_buildfromfile_params()
  212. test_vectors_from_file_all_buildfromfile_params_eager()
  213. test_vectors_from_file_eager()
  214. test_vectors_invalid_input()