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_prelu_cell.py 2.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2019 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. import numpy as np
  15. import mindspore as ms
  16. import mindspore.nn as nn
  17. from mindspore import Tensor
  18. from mindspore import context
  19. from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
  20. from mindspore.nn.optim.momentum import Momentum
  21. from mindspore.train import Model
  22. from mindspore.context import ParallelMode
  23. from mindspore.nn import PReLU
  24. from tests.dataset_mock import MindData
  25. context.set_context(mode=context.GRAPH_MODE)
  26. class Dataset(MindData):
  27. def __init__(self, predict, label, length=3, input_num=2):
  28. super(Dataset, self).__init__(size=length)
  29. self.predict = predict
  30. self.label = label
  31. self.index = 0
  32. self.length = length
  33. self.input_num = input_num
  34. def __iter__(self):
  35. return self
  36. def __next__(self):
  37. if self.index >= self.length:
  38. raise StopIteration
  39. self.index += 1
  40. if self.input_num == 2:
  41. return (self.predict, self.label)
  42. return (self.predict,)
  43. def reset(self):
  44. self.index = 0
  45. class PReLUNet(nn.Cell):
  46. def __init__(self):
  47. super(PReLUNet, self).__init__()
  48. self.prelu = PReLU(channel=256)
  49. def construct(self, x):
  50. x = self.prelu(x)
  51. return x
  52. def reshape_common(parallel_mode):
  53. learning_rate = 0.1
  54. momentum = 0.9
  55. epoch_size = 2
  56. context.reset_auto_parallel_context()
  57. context.set_auto_parallel_context(parallel_mode=parallel_mode, device_num=8)
  58. predict = Tensor(np.ones([32, 256]), dtype=ms.float32)
  59. label = Tensor(np.ones([32]), dtype=ms.int32)
  60. dataset = Dataset(predict, label, 2)
  61. net = PReLUNet()
  62. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  63. opt = Momentum(net.trainable_params(), learning_rate, momentum)
  64. model = Model(net, loss, opt)
  65. model.train(epoch_size, dataset, dataset_sink_mode=False)
  66. def test_prelu_cell():
  67. """
  68. Feature: distribute operator prelu in auto parallel.
  69. Description: prelu net with strategy in semi auto parallel.
  70. Expectation: compile done without error.
  71. """
  72. reshape_common(ParallelMode.SEMI_AUTO_PARALLEL)