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_lazyadam.py 3.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 lazy adam """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Parameter
  20. from mindspore.common.api import _executor
  21. from mindspore.nn import TrainOneStepCell, WithLossCell
  22. from mindspore.nn.optim import LazyAdam
  23. from mindspore.ops import operations as P
  24. class Net(nn.Cell):
  25. """ Net definition """
  26. def __init__(self):
  27. super(Net, self).__init__()
  28. self.weight = Parameter(Tensor(np.ones([64, 10]).astype(np.float32)), name="weight")
  29. self.bias = Parameter(Tensor(np.ones([10]).astype((np.float32))), name="bias")
  30. self.matmul = P.MatMul()
  31. self.biasAdd = P.BiasAdd()
  32. def construct(self, x):
  33. x = self.biasAdd(self.matmul(x, self.weight), self.bias)
  34. return x
  35. class NetWithSparseGatherV2(nn.Cell):
  36. """ NetWithSparseGatherV2 definition """
  37. def __init__(self):
  38. super(NetWithSparseGatherV2, self).__init__()
  39. self.weight1 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight1", sparse_grad=True)
  40. self.weight2 = Parameter(Tensor(np.ones([2, 1, 2]).astype((np.float32))), name="weight2")
  41. self.axis = 0
  42. self.gather = P.SparseGatherV2()
  43. def construct(self, indices, label):
  44. return self.gather(self.weight1, indices, self.axis) + self.weight2
  45. def test_lazy_adam_compile():
  46. """ test lazy adam compile """
  47. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  48. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  49. net = Net()
  50. net.set_train()
  51. loss = nn.SoftmaxCrossEntropyWithLogits()
  52. optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9)
  53. net_with_loss = WithLossCell(net, loss)
  54. train_network = TrainOneStepCell(net_with_loss, optimizer)
  55. _executor.compile(train_network, inputs, label)
  56. def test_spares_lazy_adam_compile():
  57. """ test sparse adam compile """
  58. indices = Tensor(np.array([0, 1]).astype(np.int32))
  59. label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
  60. net = NetWithSparseGatherV2()
  61. net.set_train()
  62. optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1)
  63. train_network = TrainOneStepCell(net, optimizer)
  64. _executor.compile(train_network, indices, label)
  65. def test_lazy_adam_error():
  66. net = Net()
  67. with pytest.raises(ValueError):
  68. LazyAdam(net.get_parameters(), learning_rate=-0.1)
  69. with pytest.raises(TypeError):
  70. LazyAdam(net.get_parameters(), learning_rate=0.1, beta1=2)