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_cell_wrapper.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright 2019 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.nn as nn
  18. from mindspore import Tensor, Parameter
  19. from mindspore.common import dtype as mstype
  20. from mindspore.common.api import _executor
  21. from mindspore.nn import TrainOneStepCell, WithLossCell, ParameterUpdate
  22. from mindspore.nn.optim import Momentum
  23. from mindspore.ops import operations as P
  24. class Net(nn.Cell):
  25. def __init__(self):
  26. super(Net, self).__init__()
  27. self.weight = Parameter(Tensor(np.ones([64, 10]).astype(np.float32)), name="weight")
  28. self.bias = Parameter(Tensor(np.ones([10]).astype((np.float32))), name="bias")
  29. self.matmul = P.MatMul()
  30. self.biasAdd = P.BiasAdd()
  31. def construct(self, x):
  32. x = self.biasAdd(self.matmul(x, self.weight), self.bias)
  33. return x
  34. def test_parameter_update_int32_and_tensor():
  35. """ test_parameter_update """
  36. net = Net()
  37. loss = nn.SoftmaxCrossEntropyWithLogits()
  38. optimizer = Momentum(net.get_parameters(), Tensor(np.array([0.1, 0.01, 0.001]), mstype.float32), 0.001)
  39. net_with_loss = WithLossCell(net, loss)
  40. train_network = TrainOneStepCell(net_with_loss, optimizer)
  41. # compile train graph
  42. train_network.set_train()
  43. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  44. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  45. _executor.compile(train_network, inputs, label)
  46. # test tensor
  47. param_lr = train_network.parameters_dict()['learning_rate']
  48. update_network = ParameterUpdate(param_lr)
  49. update_network.phase = 'update_param'
  50. input_lr = Tensor(np.array([0.2, 0.02, 0.002]), mstype.float32)
  51. _executor.compile(update_network, input_lr)
  52. # test int32
  53. param_step = train_network.parameters_dict()['global_step']
  54. update_global_step = ParameterUpdate(param_step)
  55. input_step = Tensor(np.array([1000]), mstype.int32)
  56. _executor.compile(update_global_step, input_step)
  57. def test_parameter_update_float32():
  58. """ test_parameter_update """
  59. net = Net()
  60. loss = nn.SoftmaxCrossEntropyWithLogits()
  61. optimizer = Momentum(net.get_parameters(), 0.01, 0.001)
  62. net_with_loss = WithLossCell(net, loss)
  63. train_network = TrainOneStepCell(net_with_loss, optimizer)
  64. # compile train graph
  65. train_network.set_train()
  66. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  67. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  68. _executor.compile(train_network, inputs, label)
  69. # construct and compile update graph
  70. param_lr = train_network.parameters_dict()['learning_rate']
  71. update_network = ParameterUpdate(param_lr)
  72. update_network.phase = 'update_param'
  73. input_lr = Tensor(0.0001, mstype.float32)
  74. _executor.compile(update_network, input_lr)
  75. def test_parameter_update_error():
  76. """ test_parameter_update """
  77. input_np = np.array([1])
  78. with pytest.raises(TypeError):
  79. ParameterUpdate(input_np)