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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.nn import Dense
  21. from mindspore.nn import TrainOneStepCell, WithLossCell
  22. from mindspore.nn.optim import Adam
  23. from mindspore.ops import operations as P
  24. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  25. class NetAdam(nn.Cell):
  26. def __init__(self):
  27. super(NetAdam, self).__init__()
  28. self.batch_size = 1
  29. self.reshape = P.Reshape()
  30. weight = Tensor(np.ones([10, 16]).astype(np.float32) * 0.01)
  31. self.fc1 = Dense(16, 10, weight_init=weight)
  32. def construct(self, input_x):
  33. output = self.reshape(input_x, (self.batch_size, -1))
  34. output = self.fc1(output)
  35. return output
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_gpu_training
  38. @pytest.mark.env_onecard
  39. def test_adam():
  40. epoch = 3
  41. net = NetAdam()
  42. optimizer = Adam(filter(lambda x: x.requires_grad,
  43. net.get_parameters()), learning_rate=0.01)
  44. criterion = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  45. net_with_criterion = WithLossCell(net, criterion)
  46. train_network = TrainOneStepCell(
  47. net_with_criterion, optimizer)
  48. train_network.set_train()
  49. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  50. losses1 = []
  51. for _ in range(epoch):
  52. data = Tensor(np.arange(0, 16).reshape(
  53. 1, 1, 4, 4).astype(np.float32) * 0.01)
  54. label = Tensor(np.array([0]).astype(np.int32))
  55. loss = train_network(data, label)
  56. losses1.append(loss.asnumpy())
  57. assert losses1[0] > losses1[1]
  58. assert losses1[1] > losses1[2]
  59. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  60. losses2 = []
  61. for _ in range(epoch):
  62. data = Tensor(np.arange(0, 16).reshape(
  63. 1, 1, 4, 4).astype(np.float32) * 0.01)
  64. label = Tensor(np.array([0]).astype(np.int32))
  65. loss = train_network(data, label)
  66. losses2.append(loss.asnumpy())
  67. assert losses2[0] > losses2[1]
  68. assert losses2[1] > losses2[2]