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_embedding.py 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_embedding """
  16. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore import dtype as mstype
  19. from mindspore.model_zoo.Bert_NEZHA import EmbeddingLookup, EmbeddingPostprocessor
  20. from ..ut_filter import non_graph_engine
  21. @non_graph_engine
  22. def test_check_embedding_lookup_1():
  23. m = EmbeddingLookup(vocab_size=32000,
  24. embedding_size=768,
  25. embedding_shape=[1, 128, 768],
  26. use_one_hot_embeddings=False)
  27. m(Tensor(np.ones([128]), mstype.int32))
  28. @non_graph_engine
  29. def test_check_embedding_lookup_2():
  30. m = EmbeddingLookup(vocab_size=32000,
  31. embedding_size=768,
  32. embedding_shape=[1, 128, 768],
  33. use_one_hot_embeddings=True)
  34. m(Tensor(np.ones([128]), mstype.int32))
  35. @non_graph_engine
  36. def test_check_embedding_lookup_3():
  37. m = EmbeddingLookup(vocab_size=32000,
  38. embedding_size=768,
  39. embedding_shape=[1, 128, 768],
  40. use_one_hot_embeddings=True,
  41. initializer_range=0.01)
  42. m(Tensor(np.ones([128]), mstype.int32))
  43. @non_graph_engine
  44. def test_embedding_post_1():
  45. m = EmbeddingPostprocessor(embedding_size=768,
  46. embedding_shape=[1, 128, 768],
  47. use_token_type=True)
  48. m(Tensor(np.ones([128]), mstype.int32), Tensor(np.ones([1, 128, 768]), mstype.float32))
  49. @non_graph_engine
  50. def test_embedding_post_2():
  51. m = EmbeddingPostprocessor(embedding_size=768,
  52. embedding_shape=[1, 128, 768],
  53. use_token_type=True,
  54. initializer_range=0.3)
  55. m(Tensor(np.ones([128]), mstype.int32), Tensor(np.ones([1, 128, 768]), mstype.float32))