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_nn_embedding.py 3.5 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2020 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 nn embedding """
  16. import numpy as np
  17. import pytest
  18. from mindspore import Tensor
  19. from mindspore.common import dtype
  20. from mindspore.common.api import _executor
  21. from mindspore.nn import Embedding, MultiFieldEmbeddingLookup
  22. from ..ut_filter import non_graph_engine
  23. @non_graph_engine
  24. def test_check_embedding_1():
  25. net = Embedding(20000, 768, False)
  26. input_data = Tensor(np.ones([8, 128]), dtype.int32)
  27. _executor.compile(net, input_data)
  28. @non_graph_engine
  29. def test_check_embedding_2():
  30. net = Embedding(20000, 768, True)
  31. input_data = Tensor(np.ones([8, 128]), dtype.int32)
  32. _executor.compile(net, input_data)
  33. @non_graph_engine
  34. def test_check_embedding_3():
  35. net = Embedding(20000, 768, True, "zeros")
  36. input_data = Tensor(np.ones([8, 128]), dtype.int32)
  37. _executor.compile(net, input_data)
  38. def compile_multi_field_embedding(shape_id, shape_value, shape_field,
  39. type_id, type_value, type_field):
  40. net = MultiFieldEmbeddingLookup(20000, 768, 3)
  41. input_data = Tensor(np.ones(shape_id), type_id)
  42. input_value = Tensor(np.ones(shape_value), type_value)
  43. input_field = Tensor(np.ones(shape_field), type_field)
  44. _executor.compile(net, input_data, input_value, input_field)
  45. @non_graph_engine
  46. def test_check_multifield_embedding_right_type():
  47. compile_multi_field_embedding((8, 200), (8, 200), (8, 200),
  48. dtype.int64, dtype.float32, dtype.int32)
  49. @non_graph_engine
  50. def test_check_multifield_embedding_false_type_input():
  51. with pytest.raises(TypeError):
  52. compile_multi_field_embedding((8, 200), (8, 200), (8, 200),
  53. dtype.int16, dtype.float32, dtype.int32)
  54. @non_graph_engine
  55. def test_check_multifield_embedding_false_type_value():
  56. with pytest.raises(TypeError):
  57. compile_multi_field_embedding((8, 200), (8, 200), (8, 200),
  58. dtype.int16, dtype.float16, dtype.int32)
  59. @non_graph_engine
  60. def test_check_multifield_embedding_false_type_field_id():
  61. with pytest.raises(TypeError):
  62. compile_multi_field_embedding((8, 200), (8, 200), (8, 200),
  63. dtype.int16, dtype.float32, dtype.int16)
  64. @non_graph_engine
  65. def test_check_multifield_embedding_false_input_shape():
  66. with pytest.raises(TypeError):
  67. compile_multi_field_embedding((8,), (8, 200), (8, 200),
  68. dtype.int16, dtype.float32, dtype.int16)
  69. @non_graph_engine
  70. def test_check_multifield_embedding_false_value_shape():
  71. with pytest.raises(TypeError):
  72. compile_multi_field_embedding((8, 200), (8,), (8, 200),
  73. dtype.int16, dtype.float32, dtype.int16)
  74. @non_graph_engine
  75. def test_print_embedding():
  76. net = Embedding(20000, 768, False)
  77. print(net)