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_bprop.py 3.4 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. """ test_bprop """
  16. import numpy as np
  17. import mindspore.nn as nn
  18. from mindspore import context
  19. from mindspore.ops import operations as P
  20. from mindspore.common.parameter import Parameter
  21. from mindspore.common import Tensor
  22. from ....mindspore_test_framework.utils.bprop_util import bprop
  23. from mindspore.common.api import ms_function
  24. def setup_module(module):
  25. context.set_context(mode=context.PYNATIVE_MODE)
  26. class Net(nn.Cell):
  27. """ Net definition """
  28. def __init__(self):
  29. super(Net, self).__init__()
  30. self.matmul = P.MatMul()
  31. self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z')
  32. @ms_function
  33. def construct(self, x, y):
  34. x = x * self.z
  35. out = self.matmul(x, y)
  36. return x, out
  37. def test_bprop_no_sens():
  38. grads = bprop(Net(), Tensor(np.ones([2, 3]).astype(np.float32)),
  39. Tensor(np.ones([3, 2]).astype(np.float32)), wrt=['inputs'])
  40. print(grads)
  41. def test_bprop_sens():
  42. grads = bprop(Net(), Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
  43. grads_wrt_outputs=(Tensor(np.ones([2, 3]).astype(np.float32)),
  44. Tensor(np.ones([2, 2]).astype(np.float32))), wrt=['inputs'])
  45. print(grads)
  46. def test_bprop_first_only():
  47. grads = bprop(Net(), Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
  48. grads_wrt_outputs=(Tensor(np.ones([2, 3]).astype(np.float32)),
  49. Tensor(np.ones([2, 2]).astype(np.float32))))
  50. print(grads)
  51. def test_bprop_wrt_params():
  52. net = Net()
  53. grads = bprop(net, Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
  54. grads_wrt_outputs=(Tensor(np.ones([2, 3]).astype(np.float32)),
  55. Tensor(np.ones([2, 2]).astype(np.float32))),
  56. wrt=['params'],
  57. params=net.trainable_params())
  58. print(grads)
  59. def test_bprop_wrt_params_no_sens():
  60. net = Net()
  61. grads = bprop(net, Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
  62. wrt=['params'],
  63. params=net.trainable_params())
  64. print(grads)
  65. def test_bprop_wrt_inputs_and_params():
  66. net = Net()
  67. grads = bprop(net, Tensor(np.ones([2, 3]).astype(np.float32)), Tensor(np.ones([3, 2]).astype(np.float32)),
  68. grads_wrt_outputs=(Tensor(np.ones([2, 3]).astype(np.float32)),
  69. Tensor(np.ones([2, 2]).astype(np.float32))),
  70. wrt=['inputs', 'params'],
  71. params=net.trainable_params())
  72. print(grads)