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_math.py 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. from functools import partial
  10. import numpy as np
  11. from utils import opr_test
  12. import megengine.functional as F
  13. from megengine import tensor
  14. def common_test_reduce(opr, ref_opr):
  15. data1_shape = (5, 6, 7)
  16. data2_shape = (2, 9, 12)
  17. data1 = np.random.random(data1_shape).astype(np.float32)
  18. data2 = np.random.random(data2_shape).astype(np.float32)
  19. cases = [{"input": data1}, {"input": data2}]
  20. if opr not in (F.argmin, F.argmax):
  21. # test default axis
  22. opr_test(cases, opr, ref_fn=ref_opr)
  23. # test all axises in range of input shape
  24. for axis in range(-3, 3):
  25. # test keepdims False
  26. opr_test(cases, opr, ref_fn=lambda x: ref_opr(x, axis=axis), axis=axis)
  27. # test keepdims True
  28. opr_test(
  29. cases,
  30. opr,
  31. ref_fn=lambda x: ref_opr(x, axis=axis, keepdims=True),
  32. axis=axis,
  33. keepdims=True,
  34. )
  35. else:
  36. # test defaut axis
  37. opr_test(cases, opr, ref_fn=lambda x: ref_opr(x).astype(np.int32))
  38. # test all axises in range of input shape
  39. for axis in range(0, 3):
  40. opr_test(
  41. cases,
  42. opr,
  43. ref_fn=lambda x: ref_opr(x, axis=axis).astype(np.int32),
  44. axis=axis,
  45. )
  46. def test_sum():
  47. common_test_reduce(opr=F.sum, ref_opr=np.sum)
  48. def test_prod():
  49. common_test_reduce(opr=F.prod, ref_opr=np.prod)
  50. def test_mean():
  51. common_test_reduce(opr=F.mean, ref_opr=np.mean)
  52. def test_var():
  53. common_test_reduce(opr=F.var, ref_opr=np.var)
  54. def test_std():
  55. common_test_reduce(opr=F.std, ref_opr=np.std)
  56. def test_min():
  57. common_test_reduce(opr=F.min, ref_opr=np.min)
  58. def test_max():
  59. common_test_reduce(opr=F.max, ref_opr=np.max)
  60. def test_argmin():
  61. common_test_reduce(opr=F.argmin, ref_opr=np.argmin)
  62. def test_argmax():
  63. common_test_reduce(opr=F.argmax, ref_opr=np.argmax)
  64. def test_sqrt():
  65. d1_shape = (15,)
  66. d2_shape = (25,)
  67. d1 = np.random.random(d1_shape).astype(np.float32)
  68. d2 = np.random.random(d2_shape).astype(np.float32)
  69. cases = [{"input": d1}, {"input": d2}]
  70. opr_test(cases, F.sqrt, ref_fn=np.sqrt)
  71. def test_sort():
  72. data1_shape = (10, 3)
  73. data2_shape = (12, 2)
  74. data1 = np.random.random(data1_shape).astype(np.float32)
  75. data2 = np.random.random(data2_shape).astype(np.float32)
  76. output0 = [np.sort(data1), np.argsort(data1).astype(np.int32)]
  77. output1 = [np.sort(data2), np.argsort(data2).astype(np.int32)]
  78. cases = [
  79. {"input": data1, "output": output0},
  80. {"input": data2, "output": output1},
  81. ]
  82. opr_test(cases, F.sort)
  83. def test_normalize():
  84. cases = [
  85. {"input": np.random.random((2, 3, 12, 12)).astype(np.float32)} for i in range(2)
  86. ]
  87. def np_normalize(x, p=2, axis=None, eps=1e-12):
  88. if axis is None:
  89. norm = np.sum(x ** p) ** (1.0 / p)
  90. else:
  91. norm = np.sum(x ** p, axis=axis, keepdims=True) ** (1.0 / p)
  92. return x / np.clip(norm, a_min=eps, a_max=np.inf)
  93. # # Test L-2 norm along all dimensions
  94. # opr_test(cases, F.normalize, ref_fn=np_normalize)
  95. # # Test L-1 norm along all dimensions
  96. # opr_test(cases, partial(F.normalize, p=1), ref_fn=partial(np_normalize, p=1))
  97. # Test L-2 norm along the second dimension
  98. opr_test(cases, partial(F.normalize, axis=1), ref_fn=partial(np_normalize, axis=1))
  99. # Test some norm == 0
  100. cases[0]["input"][0, 0, 0, :] = 0
  101. cases[1]["input"][0, 0, 0, :] = 0
  102. opr_test(cases, partial(F.normalize, axis=3), ref_fn=partial(np_normalize, axis=3))

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