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_tanh_op.py 4.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import composite as C
  21. from mindspore.ops import operations as P
  22. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  23. class TanhNet(nn.Cell):
  24. def __init__(self):
  25. super(TanhNet, self).__init__()
  26. self.tanh = P.Tanh()
  27. def construct(self, x):
  28. return self.tanh(x)
  29. class Grad(nn.Cell):
  30. def __init__(self, network):
  31. super(Grad, self).__init__()
  32. self.grad = C.GradOperation(get_all=True, sens_param=True)
  33. self.network = network
  34. def construct(self, input_data, sens):
  35. gout = self.grad(self.network)(input_data, sens)
  36. return gout
  37. @pytest.mark.level0
  38. @pytest.mark.platform_x86_gpu_training
  39. @pytest.mark.env_onecard
  40. def test_Tanh():
  41. x_np = np.array(
  42. [[0.28522366, 0.38033979, 1.54657853, -0.98530175, -0.54365635, 0.12652203, -1.33449938, -0.27737698],
  43. [2.06282293, 0.84635078, 0.16628414, -0.91823183, -0.72023044, -0.09147043, -0.04166984, -1.5664763],
  44. [-0.17157249, 0.44260951, -0.6683391, 1.13142613, 1.5536937, -0.32799768, -0.20016545, 0.06773927]],
  45. dtype=np.float32)
  46. dy_np = np.array(
  47. [[0.44969849, -0.187879, -0.64300827, 1.36638774, 0.89930276, -0.23835229, -0.67771854, -1.88984999],
  48. [2.00418801, 2.33336475, 0.00241747, 1.31558685, 0.06768817, -2.23008804, -0.26818366, -1.26873401],
  49. [1.83694105, 0.5339005, 0.51117424, 0.49202378, -0.83297819, -0.71001219, 0.18913512, 0.65580389]],
  50. dtype=np.float32)
  51. x_ms = Tensor(x_np)
  52. dy_ms = Tensor(dy_np)
  53. net = TanhNet()
  54. grad = Grad(net)
  55. output = grad(x_ms, dy_ms)
  56. expect = [[0.41501077, -0.16312202, -0.10675912, 0.58678646, 0.67828224, -0.23457714, -0.1643468, -1.75159405],
  57. [0.12541081, 1.2251587, 0.00235184, 0.62396731, 0.04191568, -2.21153283, -0.26771853, -0.20311764],
  58. [1.78391056, 0.44159236, 0.33690308, 0.16800483, -0.13651318, -0.63878956, 0.18175511, 0.65280384]]
  59. assert np.allclose(output[0].asnumpy(), expect)
  60. @pytest.mark.level0
  61. @pytest.mark.platform_x86_gpu_training
  62. @pytest.mark.env_onecard
  63. def test_Tanh_fp16():
  64. np.random.seed(42)
  65. x_np = np.random.randn(5, 3, 6).astype(np.float16)
  66. dy_np = np.random.randn(5, 3, 6).astype(np.float16)
  67. x_ms = Tensor(x_np)
  68. dy_ms = Tensor(dy_np)
  69. net = TanhNet()
  70. grad = Grad(net)
  71. output = grad(x_ms, dy_ms)
  72. expect = [[[0.0766, 0.95, -0.474, -0.0568, -0.3713, -1.387],
  73. [0.04626, 0.1521, 0.004135, -0.1771, -1.149, -0.341],
  74. [-0.3235, -0.0666, -0.01921, 0.299, 0.7764, 0.1583]],
  75. [[0.124, -0.0157, -0.3682, -0.0252, 0.05997, 0.51],
  76. [-0.145, 0.2979, -0.01145, -1.019, 0.8125, 0.6914],
  77. [0.562, -0.0848, 1.402, -0.5386, 0.318, 0.645]],
  78. [[-0.9487, -0.04343, 0.02448, -0.4844, -0.939, 0.0666],
  79. [-1.049, 0.433, -0.1724, 0.9604, -0.6377, -0.1241],
  80. [0.7246, -0.1364, 0.2051, 1.132, -1.049, 0.1298]],
  81. [[0.104, 0.3643, -0.6562, -1.202, 0.4688, 0.1294],
  82. [0.2008, 0.3347, -0.2418, 0.07135, 0.1611, -0.1667],
  83. [1.856, 0.1979, -1.048, 0.4443, -0.8574, 0.1329]],
  84. [[1.156, -0.1322, 0.02069, 0.2241, 0.8164, 1.736],
  85. [-0.2433, -0.05484, -0.848, -0.7197, -0.01453, 0.2637],
  86. [0.1528, 0.6494, 0.006195, 1.307, -0.2024, 2.113]]]
  87. assert np.allclose(output[0].asnumpy(), expect, rtol=1e-3, atol=1e-3)