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_rmsprop.py 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 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 RMSProp
  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. def test_rmsprop_compile():
  36. """ test_adamw_compile """
  37. inputs = Tensor(np.ones([1, 64]).astype(np.float32))
  38. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  39. net = Net()
  40. net.set_train()
  41. loss = nn.SoftmaxCrossEntropyWithLogits()
  42. optimizer = RMSProp(net.trainable_params(), learning_rate=0.1)
  43. net_with_loss = WithLossCell(net, loss)
  44. train_network = TrainOneStepCell(net_with_loss, optimizer)
  45. _executor.compile(train_network, inputs, label)
  46. def test_rmsprop_e():
  47. net = Net()
  48. with pytest.raises(ValueError):
  49. RMSProp(net.get_parameters(), momentum=-0.1, learning_rate=0.1)
  50. with pytest.raises(TypeError):
  51. RMSProp(net.get_parameters(), momentum=1, learning_rate=0.1)