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

5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_training """
  16. import numpy as np
  17. import mindspore.nn as nn
  18. from mindspore import context
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.nn import WithGradCell, WithLossCell
  21. from mindspore.ops import operations as P
  22. from ..ut_filter import non_graph_engine
  23. def setup_module(module):
  24. context.set_context(mode=context.PYNATIVE_MODE)
  25. class LeNet5(nn.Cell):
  26. """ LeNet5 definition """
  27. def __init__(self):
  28. super(LeNet5, self).__init__()
  29. self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
  30. self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
  31. self.fc1 = nn.Dense(16 * 5 * 5, 120)
  32. self.fc2 = nn.Dense(120, 84)
  33. self.fc3 = nn.Dense(84, 10)
  34. self.relu = nn.ReLU()
  35. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  36. self.flatten = P.Flatten()
  37. def construct(self, x):
  38. x = self.max_pool2d(self.relu(self.conv1(x)))
  39. x = self.max_pool2d(self.relu(self.conv2(x)))
  40. x = self.flatten(x)
  41. x = self.relu(self.fc1(x))
  42. x = self.relu(self.fc2(x))
  43. x = self.fc3(x)
  44. return x
  45. @non_graph_engine
  46. def test_loss_cell_wrapper():
  47. """ test_loss_cell_wrapper """
  48. data = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
  49. label = Tensor(np.ones([1, 10]).astype(np.float32))
  50. net = LeNet5()
  51. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  52. loss_net = WithLossCell(net, loss_fn)
  53. loss_out = loss_net(data, label)
  54. assert loss_out.asnumpy().dtype == 'float32' or loss_out.asnumpy().dtype == 'float64'
  55. @non_graph_engine
  56. def test_grad_cell_wrapper():
  57. """ test_grad_cell_wrapper """
  58. data = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
  59. label = Tensor(np.ones([1, 10]).astype(np.float32))
  60. dout = Tensor(np.ones([1]).astype(np.float32))
  61. net = LeNet5()
  62. loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  63. grad_net = WithGradCell(net, loss_fn, dout)
  64. gradients = grad_net(data, label)
  65. assert isinstance(gradients[0].asnumpy()[0][0][0][0], (np.float32, np.float64))