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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 mindspore.nn as nn
  17. import mindspore.ops as ops
  18. from mindspore import context
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. from mindspore.ops import composite as C
  22. grad_all = C.GradOperation(get_all=True)
  23. class CropAndResizeNet(nn.Cell):
  24. def __init__(self, crop_size):
  25. super(CropAndResizeNet, self).__init__()
  26. self.crop_and_resize = P.CropAndResize()
  27. self.crop_size = crop_size
  28. def construct(self, x, boxes, box_indices):
  29. return self.crop_and_resize(x, boxes, box_indices, self.crop_size)
  30. def bprop(self, x, boxes, box_indices, out, dout):
  31. return x, boxes, box_indices
  32. class TestUserDefinedBpropNet(nn.Cell):
  33. def __init__(self, in_channel, out_channel):
  34. super(TestUserDefinedBpropNet, self).__init__()
  35. self.relu = nn.ReLU()
  36. self.conv = nn.Conv2d(in_channels=in_channel, out_channels=out_channel, kernel_size=2, stride=1, has_bias=False,
  37. weight_init='ones', pad_mode='same')
  38. self.crop = CropAndResizeNet((10, 10))
  39. self.boxes = Tensor(np.ones((128, 4)).astype(np.float32))
  40. self.box_indices = Tensor(np.ones((128,)).astype(np.int32))
  41. def construct(self, x):
  42. x = self.relu(x)
  43. x = self.conv(x)
  44. x = self.crop(x, self.boxes, self.box_indices)
  45. return x
  46. class TestUserDefinedBpropGradNet(nn.Cell):
  47. def __init__(self, net):
  48. super(TestUserDefinedBpropGradNet, self).__init__()
  49. self.net = net
  50. def construct(self, x):
  51. return grad_all(self.net)(x)
  52. def test_user_defined_bprop():
  53. context.set_context(mode=context.GRAPH_MODE)
  54. net = TestUserDefinedBpropNet(3, 10)
  55. grad_net = TestUserDefinedBpropGradNet(net)
  56. x = Tensor(np.ones((128, 3, 12, 12)).astype(np.float32))
  57. grad_net(x)
  58. class SinNet(nn.Cell):
  59. def __init__(self):
  60. super(SinNet, self).__init__()
  61. self.sin = ops.Sin()
  62. def construct(self, x):
  63. out = self.sin(x)
  64. return out
  65. class SinGrad(nn.Cell):
  66. def __init__(self, network):
  67. super(SinGrad, self).__init__()
  68. self.grad = ops.GradOperation()
  69. self.network = network
  70. def construct(self, x):
  71. gout = self.grad(self.network)(x)
  72. return gout
  73. class SinGradSec(nn.Cell):
  74. def __init__(self, network):
  75. super(SinGradSec, self).__init__()
  76. self.grad = ops.GradOperation()
  77. self.network = network
  78. def construct(self, x):
  79. gout = self.grad(self.network)(x)
  80. return gout
  81. def test_second_grad_with_j_primitive():
  82. context.set_context(mode=context.GRAPH_MODE)
  83. net = SinNet()
  84. first_grad = SinGrad(net)
  85. second_grad = SinGradSec(first_grad)
  86. x = Tensor(np.array([1.0], dtype=np.float32))
  87. second_grad(x)