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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. # ============================================================================
  15. import pytest
  16. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. from mindspore.ops.operations import _grad_ops as G
  19. import mindspore.nn as nn
  20. import numpy as np
  21. import mindspore.context as context
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common.parameter import Parameter
  24. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  25. class Net_Pool_Grad(nn.Cell):
  26. def __init__(self):
  27. super(Net_Pool_Grad, self).__init__()
  28. self.maxpool_grad_fun = G.MaxPoolGrad(padding="VALID",
  29. ksize=2,
  30. strides=2)
  31. self.x = Parameter(initializer(
  32. Tensor(np.array([[[
  33. [0, 1, 2, 3, 4, 5],
  34. [6, 7, 8, 9, 10, 11],
  35. [12, 13, 14, 15, 16, 17],
  36. [18, 19, 20, 21, 22, 23],
  37. [24, 25, 26, 27, 28, 29],
  38. [30, 31, 32, 33, 34, 35]
  39. ]]]).astype(np.float32)), [1, 1, 6, 6]), name='x')
  40. self.a = Parameter(initializer(
  41. Tensor(np.array([[[
  42. [3, 3, 3],
  43. [3, 3, 3],
  44. [3, 3, 3]
  45. ]]]).astype(np.float32)), [1, 1, 3, 3]), name='a')
  46. self.d = Parameter(initializer(
  47. Tensor(np.array([[[
  48. [7, 9, 11],
  49. [19, 21, 23],
  50. [31, 33, 35]
  51. ]]]).astype(np.float32)), [1, 1, 3, 3]), name='d')
  52. def construct(self):
  53. return self.maxpool_grad_fun(self.x, self.a, self.d)
  54. @pytest.mark.level0
  55. @pytest.mark.platform_x86_cpu
  56. @pytest.mark.env_onecard
  57. def test_maxpool2d_grad():
  58. maxpool2d_grad = Net_Pool_Grad()
  59. output = maxpool2d_grad()
  60. print(output)
  61. expect_result = (np.array([[[
  62. [0, 0, 0, 0, 0, 0],
  63. [0, 7, 0, 9, 0, 11],
  64. [0, 0, 0, 0, 0, 0],
  65. [0, 19, 0, 21, 0, 23],
  66. [0, 0, 0, 0, 0, 0],
  67. [0, 31, 0, 33, 0, 35]
  68. ]]]))
  69. assert (output.asnumpy() == expect_result).all()