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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. print(data)
  95. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  96. res_array = np.array(res[ind], dtype=np.float32)
  97. assert np.array_equal(res_array, d["text"]), ind
  98. ind += 1
  99. def test_vectors_from_file_all_buildfromfile_params_eager():
  100. """
  101. Feature: Vectors
  102. Description: test with all parameters which include `path` and `max_vector` in function BuildFromFile in eager mode
  103. Expectation: output is equal to the expected value
  104. """
  105. vectors = text.Vectors.from_file(DATASET_ROOT_PATH + "vectors.txt", max_vectors=4)
  106. to_vectors = T.ToVectors(vectors)
  107. result1 = to_vectors("ok")
  108. result2 = to_vectors("!")
  109. result3 = to_vectors("this")
  110. result4 = to_vectors("is")
  111. result5 = to_vectors("my")
  112. result6 = to_vectors("home")
  113. result7 = to_vectors("none")
  114. res = [[0.418, 0.24968, -0.41242, 0.1217, 0.34527, -0.04445718411],
  115. [0.013441, 0.23682, -0.16899, 0.40951, 0.63812, 0.47709],
  116. [0.15164, 0.30177, -0.16763, 0.17684, 0.31719, 0.33973],
  117. [0.70853, 0.57088, -0.4716, 0.18048, 0.54449, 0.72603],
  118. [0, 0, 0, 0, 0, 0],
  119. [0, 0, 0, 0, 0, 0],
  120. [0, 0, 0, 0, 0, 0]]
  121. res_array = np.array(res, dtype=np.float32)
  122. assert np.array_equal(result1, res_array[0])
  123. assert np.array_equal(result2, res_array[1])
  124. assert np.array_equal(result3, res_array[2])
  125. assert np.array_equal(result4, res_array[3])
  126. assert np.array_equal(result5, res_array[4])
  127. assert np.array_equal(result6, res_array[5])
  128. assert np.array_equal(result7, res_array[6])
  129. def test_vectors_from_file_eager():
  130. """
  131. Feature: Vectors
  132. Description: test with only default parameter in eager mode
  133. Expectation: output is equal to the expected value
  134. """
  135. vectors = text.Vectors.from_file(DATASET_ROOT_PATH + "vectors.txt")
  136. to_vectors = T.ToVectors(vectors)
  137. result1 = to_vectors("ok")
  138. result2 = to_vectors("!")
  139. result3 = to_vectors("this")
  140. result4 = to_vectors("is")
  141. result5 = to_vectors("my")
  142. result6 = to_vectors("home")
  143. result7 = to_vectors("none")
  144. res = [[0.418, 0.24968, -0.41242, 0.1217, 0.34527, -0.04445718411],
  145. [0.013441, 0.23682, -0.16899, 0.40951, 0.63812, 0.47709],
  146. [0.15164, 0.30177, -0.16763, 0.17684, 0.31719, 0.33973],
  147. [0.70853, 0.57088, -0.4716, 0.18048, 0.54449, 0.72603],
  148. [0.68047, -0.039263, 0.30186, -0.17792, 0.42962, 0.032246],
  149. [0.26818, 0.14346, -0.27877, 0.016257, 0.11384, 0.69923],
  150. [0, 0, 0, 0, 0, 0]]
  151. res_array = np.array(res, dtype=np.float32)
  152. assert np.array_equal(result1, res_array[0])
  153. assert np.array_equal(result2, res_array[1])
  154. assert np.array_equal(result3, res_array[2])
  155. assert np.array_equal(result4, res_array[3])
  156. assert np.array_equal(result5, res_array[4])
  157. assert np.array_equal(result6, res_array[5])
  158. assert np.array_equal(result7, res_array[6])
  159. def test_vectors_invalid_input():
  160. """
  161. Feature: Vectors
  162. Description: test the validate function with invalid parameters.
  163. Expectation:
  164. """
  165. def test_invalid_input(test_name, file_path, error, error_msg, max_vectors=None,
  166. unk_init=None, lower_case_backup=False, token="ok"):
  167. log.info("Test Vectors with wrong input: {0}".format(test_name))
  168. with pytest.raises(error) as error_info:
  169. vectors = text.Vectors.from_file(file_path, max_vectors=max_vectors)
  170. to_vectors = T.ToVectors(vectors, unk_init=unk_init, lower_case_backup=lower_case_backup)
  171. to_vectors(token)
  172. assert error_msg in str(error_info.value)
  173. test_invalid_input("Not all vectors have the same number of dimensions",
  174. DATASET_ROOT_PATH + "vectors_dim_different.txt", error=RuntimeError,
  175. error_msg="all vectors must have the same number of dimensions, but got dim 5 while expecting 6")
  176. test_invalid_input("the file is empty.", DATASET_ROOT_PATH + "vectors_empty.txt",
  177. error=RuntimeError, error_msg="invalid file, file is empty.")
  178. test_invalid_input("the count of `unknown_init`'s element is different with word vector.",
  179. DATASET_ROOT_PATH + "vectors.txt",
  180. error=RuntimeError, error_msg="Unexpected error. ToVectors: " +
  181. "unk_init must be the same length as vectors, but got unk_init: 2 and vectors: 6",
  182. unk_init=[-1, -1])
  183. test_invalid_input("The file not exist", DATASET_ROOT_PATH + "not_exist.txt", error=RuntimeError,
  184. error_msg="get real path failed")
  185. test_invalid_input("The token is 1-dimensional",
  186. DATASET_ROOT_PATH + "vectors_with_wrong_info.txt", error=RuntimeError,
  187. error_msg="token with 1-dimensional vector.")
  188. test_invalid_input("max_vectors parameter must be greater than 0",
  189. DATASET_ROOT_PATH + "vectors.txt", error=ValueError,
  190. error_msg="Input max_vectors is not within the required interval", max_vectors=-1)
  191. test_invalid_input("invalid max_vectors parameter type as a float",
  192. DATASET_ROOT_PATH + "vectors.txt", error=TypeError,
  193. error_msg="Argument max_vectors with value 1.0 is not of type [<class 'int'>],"
  194. " but got <class 'float'>.", max_vectors=1.0)
  195. test_invalid_input("invalid max_vectors parameter type as a string",
  196. DATASET_ROOT_PATH + "vectors.txt", error=TypeError,
  197. error_msg="Argument max_vectors with value 1 is not of type [<class 'int'>],"
  198. " but got <class 'str'>.", max_vectors="1")
  199. test_invalid_input("invalid token parameter type as a float", DATASET_ROOT_PATH + "vectors.txt", error=RuntimeError,
  200. error_msg="input tensor type should be string.", token=1.0)
  201. test_invalid_input("invalid lower_case_backup parameter type as a string", DATASET_ROOT_PATH + "vectors.txt",
  202. error=TypeError, error_msg="Argument lower_case_backup with " +
  203. "value True is not of type [<class 'bool'>],"
  204. " but got <class 'str'>.", lower_case_backup="True")
  205. test_invalid_input("invalid lower_case_backup parameter type as a string", DATASET_ROOT_PATH + "vectors.txt",
  206. error=TypeError, error_msg="Argument lower_case_backup with " +
  207. "value True is not of type [<class 'bool'>],"
  208. " but got <class 'str'>.", lower_case_backup="True")
  209. if __name__ == '__main__':
  210. test_vectors_all_tovectors_params_eager()
  211. test_vectors_from_file()
  212. test_vectors_from_file_all_buildfromfile_params()
  213. test_vectors_from_file_all_buildfromfile_params_eager()
  214. test_vectors_from_file_eager()
  215. test_vectors_invalid_input()