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 2.8 kB

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