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_lenet.py 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 lenet"""
  16. import numpy as np
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.common.api import _executor
  21. from mindspore.ops import operations as P
  22. from ....train_step_wrap import train_step_with_loss_warp, train_step_with_sens
  23. context.set_context(mode=context.GRAPH_MODE)
  24. class LeNet5(nn.Cell):
  25. """LeNet5 definition"""
  26. def __init__(self):
  27. super(LeNet5, self).__init__()
  28. self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
  29. self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
  30. self.fc1 = nn.Dense(16 * 5 * 5, 120)
  31. self.fc2 = nn.Dense(120, 84)
  32. self.fc3 = nn.Dense(84, 10)
  33. self.relu = nn.ReLU()
  34. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  35. self.flatten = P.Flatten()
  36. def construct(self, x):
  37. x = self.max_pool2d(self.relu(self.conv1(x)))
  38. x = self.max_pool2d(self.relu(self.conv2(x)))
  39. x = self.flatten(x)
  40. x = self.relu(self.fc1(x))
  41. x = self.relu(self.fc2(x))
  42. x = self.fc3(x)
  43. return x
  44. def test_lenet5_train_step():
  45. predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
  46. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  47. net = train_step_with_loss_warp(LeNet5())
  48. _executor.compile(net, predict, label)
  49. def test_lenet5_train_sens():
  50. predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
  51. sens = Tensor(np.ones([1, 10]).astype(np.float32))
  52. net = train_step_with_sens(LeNet5(), sens)
  53. _executor.compile(net, predict)
  54. def test_lenet5_train_step_training():
  55. predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
  56. label = Tensor(np.zeros([1, 10]).astype(np.float32))
  57. net = train_step_with_loss_warp(LeNet5())
  58. net.set_train()
  59. _executor.compile(net, predict, label)