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.3 kB

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