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_hyper_param.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright 2021 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 mindspore as ms
  16. from mindspore import Tensor, Parameter
  17. from mindspore.nn import Cell
  18. def test_hyper_param():
  19. """
  20. Feature: Resolve parameter.
  21. Description: The name of parameter in construct is the same with the name of parameter of class init.
  22. Expectation: self.a is different from a in construct.
  23. """
  24. class HyperParamNet(Cell):
  25. def __init__(self):
  26. super(HyperParamNet, self).__init__()
  27. self.a = Parameter(Tensor(1, ms.float32), name="a")
  28. self.b = Parameter(Tensor(5, ms.float32), name="param_b")
  29. self.c = Parameter(Tensor(9, ms.float32), name="param_c")
  30. def func_inner(self, c):
  31. return self.a + self.b + c
  32. def construct(self, a, b):
  33. self.a = a
  34. self.b = b
  35. return self.func_inner(self.c)
  36. x = Tensor(11, ms.float32)
  37. y = Tensor(19, ms.float32)
  38. net = HyperParamNet()
  39. output = net(x, y)
  40. output_expect = Tensor(39, ms.float32)
  41. assert output == output_expect
  42. def test_hyper_param_with_control_sink():
  43. """
  44. Feature: Resolve parameter.
  45. Description: Parameters whose name are the same between different graphs do not affect each other.
  46. Expectation: self.a is different from a in construct.
  47. """
  48. class HyperParamNet(Cell):
  49. def __init__(self):
  50. super(HyperParamNet, self).__init__()
  51. self.a = Parameter(Tensor(1, ms.float32), name="a")
  52. self.b = Parameter(Tensor(5, ms.float32), name="b")
  53. self.c = Parameter(Tensor(9, ms.float32), name="c")
  54. def func_inner(self, c):
  55. return self.a + self.b + c
  56. def func_inner_2(self, a, c):
  57. return a - self.b + c
  58. def construct(self, a, b):
  59. self.b = b
  60. if a > self.b:
  61. return self.func_inner_2(a, self.c)
  62. return self.func_inner(self.c)
  63. x = Tensor(11, ms.float32)
  64. y = Tensor(19, ms.float32)
  65. net = HyperParamNet()
  66. output = net(x, y)
  67. output_expect = Tensor(29, ms.float32)
  68. assert output == output_expect