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_var_grad.py 12 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.ops.composite as C
  17. from mindspore import Tensor, Parameter
  18. from mindspore import context
  19. from mindspore.common import dtype as mstype
  20. from mindspore.common.parameter import ParameterTuple
  21. from mindspore.nn import Cell
  22. from mindspore.ops import operations as P
  23. context.set_context(mode=context.GRAPH_MODE)
  24. grad_by_list = C.GradOperation(get_by_list=True)
  25. grad_all_with_sens = C.GradOperation(get_all=True, sens_param=True)
  26. grad_by_list_with_sens = C.GradOperation(get_by_list=True, sens_param=True)
  27. grad_all = C.GradOperation(get_all=True)
  28. grad_with_sens = C.GradOperation(sens_param=True)
  29. def test_net_vargs_expand():
  30. class AddNet(Cell):
  31. def __init__(self):
  32. super(AddNet, self).__init__()
  33. self.w = Parameter(
  34. Tensor(np.ones((3, 4, 5), np.float32)), "w2", requires_grad=True)
  35. def construct(self, x, y):
  36. return x + y
  37. x = Tensor(np.random.normal(0, 1, [3, 4, 5]).astype(np.float32))
  38. y = Tensor(np.random.normal(0, 1, [3, 4, 5]).astype(np.float32))
  39. sens = Tensor(np.random.normal(0, 1, [3, 4, 5]).astype(np.float32))
  40. net = AddNet()
  41. _ = grad_all_with_sens(net, net.trainable_params())(x, y, sens)
  42. class VarNet(Cell):
  43. def __init__(self, net):
  44. super(VarNet, self).__init__()
  45. self.b = Parameter(
  46. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "b", requires_grad=True)
  47. self.w = Parameter(
  48. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "w", requires_grad=True)
  49. self.net = net
  50. def construct(self, *args):
  51. return self.net(*args) * self.w + self.b
  52. class SecondNet(Cell):
  53. def __init__(self):
  54. super(SecondNet, self).__init__()
  55. self.b2 = Parameter(
  56. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "b2", requires_grad=True)
  57. def construct(self, *args):
  58. res = args[0] + args[1]
  59. return res + self.b2
  60. class Bprop(Cell):
  61. def __init__(self, func, wrt_params, params, grad_op, sens=None):
  62. super(Bprop, self).__init__(auto_prefix=False)
  63. self.func = func
  64. self.wrt_params = wrt_params
  65. self.params = None
  66. if self.wrt_params and params:
  67. self.params = ParameterTuple(params)
  68. self.grad = grad_op
  69. self.with_sens = False
  70. self.sens = sens
  71. if not sens is None:
  72. self.sens = sens if isinstance(sens, Tensor) else Tensor(sens, dtype=mstype.float32)
  73. self.with_sens = True
  74. def construct(self, *inputs):
  75. # pylint: disable=no-else-return
  76. if self.wrt_params:
  77. if self.with_sens:
  78. return self.grad(self.func, self.params)(*inputs, self.sens)
  79. else:
  80. return self.grad(self.func, self.params)(*inputs)
  81. elif self.with_sens:
  82. return self.grad(self.func)(*inputs, self.sens)
  83. else:
  84. return self.grad(self.func)(*inputs)
  85. def test_all_var_args_grad_with_sens():
  86. """"test grad_by_list_with_sens with all var args input"""
  87. class GradNet(Cell):
  88. def __init__(self, net):
  89. super(GradNet, self).__init__()
  90. self.weights = ParameterTuple(net.trainable_params())
  91. self.net = net
  92. def construct(self, *inputs):
  93. return grad_by_list_with_sens(self.net, self.weights)(*inputs)
  94. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  95. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  96. sens = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  97. net = VarNet(SecondNet())
  98. grad_net = GradNet(net)
  99. _ = grad_net(x, y, sens)
  100. def test_grad_list_var_args():
  101. class GradNet(Cell):
  102. def __init__(self, net):
  103. super(GradNet, self).__init__()
  104. self.weights = ParameterTuple(net.trainable_params())
  105. self.net = net
  106. def construct(self, *inputs):
  107. return grad_by_list(self.net, self.weights)(*inputs)
  108. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  109. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  110. net = VarNet(SecondNet())
  111. grad_net = GradNet(net)
  112. _ = grad_net(x, y)
  113. def test_grad_all_var_args():
  114. class GradNet(Cell):
  115. def __init__(self, net):
  116. super(GradNet, self).__init__()
  117. self.weights = ParameterTuple(net.trainable_params())
  118. self.net = net
  119. def construct(self, *inputs):
  120. return grad_all(self.net)(*inputs)
  121. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  122. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  123. net = VarNet(SecondNet())
  124. grad_net = GradNet(net)
  125. _ = grad_net(x, y)
  126. def test_grad_all_var_args_with_sens():
  127. class GradNet(Cell):
  128. def __init__(self, net):
  129. super(GradNet, self).__init__()
  130. self.weights = ParameterTuple(net.trainable_params())
  131. self.net = net
  132. def construct(self, *inputs):
  133. return grad_all_with_sens(self.net)(*inputs)
  134. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  135. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  136. sens = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  137. net = VarNet(SecondNet())
  138. grad_net = GradNet(net)
  139. _ = grad_net(x, y, sens)
  140. def test_grad_var_args_with_sens():
  141. class GradNet(Cell):
  142. def __init__(self, net):
  143. super(GradNet, self).__init__()
  144. self.weights = ParameterTuple(net.trainable_params())
  145. self.net = net
  146. def construct(self, *inputs):
  147. return grad_with_sens(self.net)(*inputs)
  148. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  149. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  150. sens = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  151. net = VarNet(SecondNet())
  152. grad_net = GradNet(net)
  153. _ = grad_net(x, y, sens)
  154. def test_grad_with_param_sens():
  155. """"test grad_with_sens parameter"""
  156. class GradNet(Cell):
  157. def __init__(self, net):
  158. super(GradNet, self).__init__()
  159. self.weights = ParameterTuple(net.trainable_params())
  160. self.net = net
  161. self.sens = Parameter(Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), name='sens', requires_grad=False)
  162. self.grad = C.GradOperation(get_by_list=True, sens_param=True)
  163. def construct(self, x, y):
  164. return self.grad(self.net, self.weights)(x, y, self.sens)
  165. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  166. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  167. net = SecondNet()
  168. grad_net = GradNet(net)
  169. _ = grad_net(x, y)
  170. def test_var_args_grad():
  171. class VarNet(Cell):
  172. def __init__(self, net):
  173. super(VarNet, self).__init__()
  174. self.b = Parameter(
  175. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "b", requires_grad=True)
  176. self.net = net
  177. def construct(self, *args):
  178. return self.net(*args) + self.b
  179. class SecondNet(Cell):
  180. def __init__(self):
  181. super(SecondNet, self).__init__()
  182. self.b2 = Parameter(
  183. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "b2", requires_grad=True)
  184. def construct(self, *args):
  185. res = args[0] + args[1]
  186. return res + self.b2
  187. class GradNet(Cell):
  188. def __init__(self, net):
  189. super(GradNet, self).__init__()
  190. self.net = net
  191. self.weights = ParameterTuple(net.trainable_params())
  192. def construct(self, x, y, sens):
  193. return grad_by_list_with_sens(self.net, self.weights)(x, y, sens)
  194. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  195. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  196. sens = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  197. net = VarNet(SecondNet())
  198. grad_net = GradNet(net)
  199. _ = grad_net(x, y, sens)
  200. def test_var_args_positional():
  201. """"test grad_all with var args in inner graph"""
  202. class VarNet(Cell):
  203. def __init__(self, net):
  204. super(VarNet, self).__init__()
  205. self.net = net
  206. def construct(self, x, y):
  207. return self.net(x, y) * x
  208. class SecondNet(Cell):
  209. def __init__(self):
  210. super(SecondNet, self).__init__()
  211. def construct(self, *args):
  212. return args[0] + args[1]
  213. class GradNet(Cell):
  214. def __init__(self, net):
  215. super(GradNet, self).__init__()
  216. self.net = net
  217. self.weights = ParameterTuple(net.trainable_params())
  218. def construct(self, x, y):
  219. return grad_all(self.net)(x, y)
  220. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  221. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  222. net = VarNet(SecondNet())
  223. grad_net = GradNet(net)
  224. _ = grad_net(x, y)
  225. def test_grad_within_if_else():
  226. class GradNet(Cell):
  227. def __init__(self, net):
  228. super(GradNet, self).__init__()
  229. self.weights = ParameterTuple(net.trainable_params())
  230. self.net = net
  231. grad_op = C.GradOperation(get_all=False, get_by_list=True, sens_param=True)
  232. sens = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  233. self.grad = Bprop(self.net, True, self.weights, grad_op, sens)
  234. def construct(self, *inputs):
  235. return self.grad(*inputs)
  236. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  237. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  238. net = VarNet(SecondNet())
  239. grad_net = GradNet(net)
  240. out = grad_net(x, y)
  241. print("test_grad_var_args_with_sens out=", out)
  242. def test_grad_for_concat():
  243. class GradNet(Cell):
  244. def __init__(self, net):
  245. super(GradNet, self).__init__()
  246. self.weights = ParameterTuple(net.trainable_params())
  247. self.net = net
  248. grad_op = C.GradOperation(get_all=True, get_by_list=False, sens_param=True)
  249. self.grad = Bprop(self.net, False, self.weights, grad_op)
  250. def construct(self, *inputs):
  251. return self.grad(*inputs)
  252. class Concat(Cell):
  253. def __init__(self, axis):
  254. super().__init__()
  255. self.concat = P.Concat(axis=axis)
  256. def construct(self, *input1):
  257. return self.concat(input1)
  258. class ConcatFactory:
  259. def __init__(self, input_shape, axis, dtype=np.float32):
  260. super(ConcatFactory, self).__init__()
  261. self.inputs_np = []
  262. for s in input_shape:
  263. self.inputs_np.append(np.random.randn(*s).astype(dtype))
  264. self.axis = axis
  265. self.out_numpy = np.concatenate(self.inputs_np, axis=self.axis)
  266. self.out_grad_np = self.out_numpy
  267. def grad_mindspore_impl(self):
  268. inputs = []
  269. for i in self.inputs_np:
  270. inputs.append(Tensor(i))
  271. net = Concat(axis=self.axis)
  272. grad_net = GradNet(net)
  273. grad_net.set_train()
  274. _ = grad_net(*inputs, Tensor(self.out_grad_np))
  275. def grad_cmp(self):
  276. self.grad_mindspore_impl()
  277. fact = ConcatFactory(input_shape=(
  278. (2, 184320, 1), (2, 46080, 1), (2, 11520, 1), (2, 2880, 1), (2, 720, 1)), axis=1)
  279. fact.grad_cmp()