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_function.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. import numpy as np
  9. import megengine.optimizer as optimizer
  10. from megengine import Parameter
  11. from megengine import Tensor as tensor
  12. from megengine import tensor
  13. from megengine.core.tensor.function import Function
  14. from megengine.module import Module
  15. def test_single_input():
  16. data_shape = (9, 2, 6)
  17. av = np.random.random(data_shape).astype(np.float32)
  18. class MulFunc(Function):
  19. def forward(self, a):
  20. self.a = a
  21. return a * 10
  22. def backward(self, grad_o):
  23. return grad_o * 10
  24. class Simple(Module):
  25. def __init__(self, a):
  26. super().__init__()
  27. self.a = Parameter(a, dtype=np.float32)
  28. self.layer1 = MulFunc()
  29. def forward(self):
  30. x = self.layer1(self.a)
  31. return x
  32. net = Simple(av)
  33. optim = optimizer.SGD(net.parameters(), lr=1.0)
  34. optim.zero_grad()
  35. with optim.record():
  36. loss = net()
  37. optim.backward(loss.sum())
  38. optim.step()
  39. np.testing.assert_almost_equal(loss.numpy(), (av * 10))
  40. np.testing.assert_almost_equal(net.a.numpy(), (av - 10))
  41. def test_multi_input():
  42. data_shape = (9, 2, 6)
  43. av = np.random.random(data_shape).astype(np.float32)
  44. bv = np.random.random(data_shape).astype(np.float32)
  45. class MulFunc(Function):
  46. def forward(self, a, b):
  47. self.a = a
  48. self.b = b
  49. return a * b
  50. def backward(self, grad_o):
  51. return grad_o * self.b * 2, grad_o * self.a * 3
  52. class Simple(Module):
  53. def __init__(self, a, b):
  54. super().__init__()
  55. self.a = Parameter(a, dtype=np.float32)
  56. self.b = Parameter(b, dtype=np.float32)
  57. self.layer1 = MulFunc()
  58. def forward(self):
  59. x = self.layer1(self.a, self.b)
  60. return x
  61. net = Simple(av, bv)
  62. optim = optimizer.SGD(net.parameters(), lr=1.0)
  63. optim.zero_grad()
  64. with optim.record():
  65. loss = net()
  66. optim.backward(loss.sum())
  67. optim.step()
  68. np.testing.assert_almost_equal(loss.numpy(), (av * bv))
  69. np.testing.assert_almost_equal(net.a.numpy(), (av - 2 * bv))
  70. np.testing.assert_almost_equal(net.b.numpy(), (bv - 3 * av))
  71. def test_multi_output():
  72. data_shape = (9, 2, 6)
  73. av = np.random.random(data_shape).astype(np.float32)
  74. bv = np.random.random(data_shape).astype(np.float32)
  75. class MulFunc(Function):
  76. def forward(self, a, b):
  77. self.a = a
  78. self.b = b
  79. return a * b, a + b
  80. def backward(self, grad_1, grad_2):
  81. return grad_1 * (self.b + 1), grad_2 * (self.a + 1)
  82. class Simple(Module):
  83. def __init__(self, a, b):
  84. super().__init__()
  85. self.a = Parameter(a, dtype=np.float32)
  86. self.b = Parameter(b, dtype=np.float32)
  87. self.layer1 = MulFunc()
  88. def forward(self):
  89. x, y = self.layer1(self.a, self.b)
  90. return x + y
  91. net = Simple(av, bv)
  92. optim = optimizer.SGD(net.parameters(), lr=1.0)
  93. optim.zero_grad()
  94. with optim.record():
  95. loss = net()
  96. optim.backward(loss.sum())
  97. optim.step()
  98. np.testing.assert_almost_equal(loss.numpy(), (av * bv + av + bv), decimal=6)
  99. np.testing.assert_almost_equal(net.a.numpy(), (av - bv - 1), decimal=6)
  100. np.testing.assert_almost_equal(net.b.numpy(), (bv - av - 1), decimal=6)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台