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.8 kB

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