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_ftrl.py 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 pytest
  17. import mindspore.nn as nn
  18. from mindspore import Tensor, Parameter, context
  19. from mindspore.nn import TrainOneStepCell
  20. from mindspore.nn.optim import FTRL, LazyAdam
  21. from mindspore.ops import operations as P
  22. context.set_context(enable_sparse=True,
  23. mode=context.GRAPH_MODE,
  24. device_target="Ascend")
  25. class NetWithSparseGatherV2(nn.Cell):
  26. def __init__(self):
  27. super(NetWithSparseGatherV2, self).__init__()
  28. self.weight1 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight1")
  29. self.weight2 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight2")
  30. self.axis = 0
  31. self.gather = P.SparseGatherV2()
  32. def construct(self, indices, label):
  33. return self.gather(self.weight1, indices, self.axis) + self.weight2
  34. @pytest.mark.level0
  35. @pytest.mark.platform_arm_ascend_training
  36. @pytest.mark.platform_x86_ascend_training
  37. @pytest.mark.env_onecard
  38. def test_ftrl_net():
  39. indices = Tensor(np.array([0, 0, 1]).astype(np.int32))
  40. label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
  41. net = NetWithSparseGatherV2()
  42. optimizer = FTRL(net.trainable_params(), learning_rate=0.1, weight_decay=0.9, loss_scale=2.0)
  43. optimizer.target = 'Ascend'
  44. train_network = TrainOneStepCell(net, optimizer)
  45. output = train_network(indices, label)
  46. np.allclose(output.asnumpy(), np.array([[[2, 2]], [[2, 2]], [[2, 2]]]))
  47. np.allclose(net.weight1.asnumpy(), np.array([[[0.7884067, 0.7884067]],
  48. [[0.68213105, 0.68213105]],
  49. [[1.0, 1.0]]]))
  50. np.allclose(net.weight2.asnumpy(), np.array([[[0.6821311, 0.6821311]],
  51. [[0.6821311, 0.6821311]],
  52. [[0.6821311, 0.6821311]]]))
  53. @pytest.mark.level0
  54. @pytest.mark.platform_arm_ascend_training
  55. @pytest.mark.platform_x86_ascend_training
  56. @pytest.mark.env_onecard
  57. def test_lazy_adam_net():
  58. indices = Tensor(np.array([0, 0, 1]).astype(np.int32))
  59. label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
  60. net = NetWithSparseGatherV2()
  61. optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9, loss_scale=2.0)
  62. optimizer.target = 'Ascend'
  63. train_network = TrainOneStepCell(net, optimizer)
  64. output = train_network(indices, label)
  65. np.allclose(output.asnumpy(), np.array([[[2, 2]], [[2, 2]], [[2, 2]]]))
  66. np.allclose(net.weight1.asnumpy(), np.array([[[0.9, 0.9]], [[0.9, 0.9]], [[1.0, 1.0]]]))
  67. np.allclose(net.weight2.asnumpy(), np.array([[[0.9, 0.9]], [[0.9, 0.9]], [[0.9, 0.9]]]))