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_smoothl1loss_op.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. context.set_context(mode=context.GRAPH_MODE, device_target="GPU", save_graphs=True)
  22. @pytest.mark.level0
  23. @pytest.mark.platform_x86_gpu_training
  24. @pytest.mark.env_onecard
  25. def test_smoothl1loss():
  26. np.random.seed(42)
  27. prediction = np.random.randn(20).astype(np.float32)
  28. target = np.random.randn(20).astype(np.float32)
  29. sigma = 1.0
  30. net = nn.SmoothL1Loss(sigma)
  31. loss = net(Tensor(prediction), Tensor(target))
  32. expect = [0.46941718, 0.00382918, 0.16829303, 2.447778, 0.04812113, 0.05953304,
  33. 2.2302065, 0.07672881, 0.00860204, 0.34798968, 0.00956192, 1.818008,
  34. 0.03262977, 0.36599946, 2.047463, 0.2168481, 0.7216947, 1.7739174,
  35. 0.08826803, 1.109165]
  36. assert np.allclose(loss.asnumpy(), expect)
  37. class Grad(nn.Cell):
  38. def __init__(self, network):
  39. super(Grad, self).__init__()
  40. self.grad = C.GradOperation(name="get_all", get_all=True, sens_param=True)
  41. self.network = network
  42. def construct(self, x1, x2, sens):
  43. gout = self.grad(self.network)(x1, x2, sens)
  44. return gout
  45. @pytest.mark.level0
  46. @pytest.mark.platform_x86_gpu_training
  47. @pytest.mark.env_onecard
  48. def test_smoothl1loss_grad():
  49. np.random.seed(42)
  50. prediction = np.random.randn(20).astype(np.float32)
  51. target = np.random.randn(20).astype(np.float32)
  52. sens = np.random.randn(20).astype(np.float32)
  53. sigma = 1.0
  54. net = nn.SmoothL1Loss(sigma)
  55. grad = Grad(net)
  56. dx = grad(Tensor(prediction), Tensor(target), Tensor(sens))
  57. dx1_expect = [-0.71552587, 0.01499678, -0.06709455, -0.30110368, -0.45868093,
  58. 0.24838912, -0.46063876, 0.41411355, 0.04507046, -1.4708229,
  59. 0.04481723, 0.38508227, -0.17292616, -0.52333146, -1.0309995,
  60. 0.61330026, 0.83921754, -0.3092124, 0.1391843, -0.9755451]
  61. dx2_expect = [0.71552587, -0.01499678, 0.06709455, 0.30110368, 0.45868093,
  62. -0.24838912, 0.46063876, -0.41411355, -0.04507046, 1.4708229,
  63. -0.04481723, -0.38508227, 0.17292616, 0.52333146, 1.0309995,
  64. -0.61330026, -0.83921754, 0.3092124, -0.1391843, 0.9755451]
  65. assert np.allclose(dx[0].asnumpy(), dx1_expect)
  66. assert np.allclose(dx[1].asnumpy(), dx2_expect)