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_elemwise.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. import numpy as np
  10. import megengine.functional as F
  11. from megengine import tensor
  12. from megengine.test import assertTensorClose
  13. def test_abs():
  14. assertTensorClose(
  15. F.abs(tensor([-3.0, -4.0, -5.0])).numpy(),
  16. np.abs(np.array([-3.0, -4.0, -5.0], dtype=np.float32)),
  17. )
  18. assertTensorClose(F.abs(-3.0).numpy(), np.abs(np.float32(-3.0)))
  19. def test_multiply():
  20. assertTensorClose(
  21. F.mul(-3.0, -4.0).numpy(), np.multiply(np.float32(-3.0), np.float32(-4.0))
  22. )
  23. assertTensorClose(
  24. F.mul(tensor([3.0, 4.0]), 4.0).numpy(),
  25. np.multiply(np.array([3.0, 4.0], dtype=np.float32), 4.0),
  26. )
  27. assertTensorClose(
  28. F.mul(4.0, tensor([3.0, 4.0])).numpy(),
  29. np.multiply(4.0, np.array([3.0, 4.0], dtype=np.float32)),
  30. )
  31. assertTensorClose(
  32. F.mul(tensor([3.0, 4.0]), tensor([3.0, 4.0])).numpy(),
  33. np.multiply(
  34. np.array([3.0, 4.0], dtype=np.float32),
  35. np.array([3.0, 4.0], dtype=np.float32),
  36. ),
  37. )
  38. def test_clamp():
  39. """Fix an issue when `lower` or `upper` is 0, it will be recognized as `False` and
  40. `F.clamp` will fall into wrong conditions unexpectedly.
  41. """
  42. x = np.linspace(-6, 6, dtype="float32")
  43. assertTensorClose(F.clamp(tensor(x) + 3, 0, 6).numpy(), np.clip(x + 3, 0, 6))
  44. assertTensorClose(F.clamp(tensor(x) - 3, -6, 0).numpy(), np.clip(x - 3, -6, 0))
  45. def test_isnan():
  46. for case in [[1, float("nan"), 0]]:
  47. assertTensorClose(F.isnan(tensor(case)).numpy(), np.isnan(case))
  48. def test_isinf():
  49. for case in [[1, float("inf"), 0]]:
  50. assertTensorClose(F.isinf(tensor(case)).numpy(), np.isinf(case))
  51. def test_sign():
  52. for case in [[1, -1, 0]]:
  53. x = tensor(case)
  54. assertTensorClose(F.sign(x).numpy(), np.sign(case).astype(x.dtype))
  55. def test_cosh():
  56. np.random.seed(42)
  57. x = np.random.randn(100).astype("float32")
  58. y_np = np.cosh(x)
  59. y_mge = F.cosh(tensor(x)).numpy()
  60. np.testing.assert_allclose(y_np, y_mge, rtol=1e-5)
  61. def test_sinh():
  62. np.random.seed(42)
  63. x = np.random.randn(100).astype("float32")
  64. y_np = np.sinh(x)
  65. y_mge = F.sinh(tensor(x)).numpy()
  66. np.testing.assert_allclose(y_np, y_mge, rtol=1e-5)
  67. def test_asinh():
  68. np.random.seed(42)
  69. x = np.random.randn(100).astype("float32")
  70. y_np = np.arcsinh(x)
  71. y_mge = F.asinh(tensor(x)).numpy()
  72. np.testing.assert_almost_equal(y_np, y_mge, decimal=5)
  73. def test_acosh():
  74. x = np.arange(0, 10000).astype("float32") / 100 + 1
  75. y_np = np.arccosh(x)
  76. y_mge = F.acosh(tensor(x)).numpy()
  77. np.testing.assert_almost_equal(y_np, y_mge, decimal=6)
  78. def test_atanh():
  79. np.random.seed(42)
  80. x = np.random.rand(100).astype("float32") * 2 - 1
  81. y_np = np.arctanh(x)
  82. y_mge = F.atanh(tensor(x)).numpy()
  83. np.testing.assert_almost_equal(y_np, y_mge, decimal=5)
  84. def test_fast_tanh():
  85. np.random.seed(42)
  86. x = np.random.randn(100).astype("float32")
  87. y_np = x * (27.0 + x * x) / (27.0 + 9.0 * x * x)
  88. y_mge = F.fast_tanh(tensor(x)).numpy()
  89. np.testing.assert_almost_equal(y_np, y_mge, decimal=6)
  90. def test_hswish():
  91. np.random.seed(42)
  92. x = np.random.randn(100).astype("float32")
  93. y_np = x * np.minimum(np.maximum(x + 3, 0), 6) / 6
  94. y_mge = F.hswish(tensor(x)).numpy()
  95. np.testing.assert_almost_equal(y_np, y_mge, decimal=6)
  96. def test_hsigmoid():
  97. np.random.seed(42)
  98. x = np.random.randn(100).astype("float32")
  99. y_np = np.minimum(np.maximum(x + 3, 0), 6) / 6
  100. y_mge = F.hsigmoid(tensor(x)).numpy()
  101. np.testing.assert_equal(y_np, y_mge)
  102. def test_logical_oprs():
  103. x = np.array([[True, False], [False, True]])
  104. y = np.array([[True, True], [False, False]])
  105. xx = tensor(x)
  106. yy = tensor(y)
  107. np.testing.assert_equal(~x, (F.logical_not(xx)).numpy())
  108. np.testing.assert_equal(x & y, F.logical_and(xx, yy).numpy())
  109. np.testing.assert_equal(x | y, F.logical_or(xx, yy).numpy())
  110. np.testing.assert_equal(x ^ y, F.logical_xor(xx, yy).numpy())

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