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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 as mge
  11. import megengine.functional as F
  12. import megengine.jit as jit
  13. import megengine.module as M
  14. import megengine.random as R
  15. def test_random_static_diff_result():
  16. @jit.trace(symbolic=True)
  17. def graph_a():
  18. return R.uniform(5) + R.gaussian(5)
  19. @jit.trace(symbolic=True)
  20. def graph_b():
  21. return R.uniform(5) + R.gaussian(5)
  22. a = graph_a()
  23. b = graph_b()
  24. assert np.any(a.numpy() != b.numpy())
  25. def test_random_static_same_result():
  26. @jit.trace(symbolic=True)
  27. def graph_a():
  28. R.manual_seed(731)
  29. return R.uniform(5) + R.gaussian(5)
  30. @jit.trace(symbolic=True)
  31. def graph_b():
  32. R.manual_seed(731)
  33. return R.uniform(5) + R.gaussian(5)
  34. a = graph_a()
  35. b = graph_b()
  36. assert np.all(a.numpy() == b.numpy())
  37. def test_random_dynamic_diff_result():
  38. a = R.uniform(5) + R.gaussian(5)
  39. b = R.uniform(5) + R.gaussian(5)
  40. assert np.any(a.numpy() != b.numpy())
  41. def test_random_dynamic_same_result():
  42. R.manual_seed(0)
  43. a = R.uniform(5) + R.gaussian(5)
  44. R.manual_seed(0)
  45. b = R.uniform(5) + R.gaussian(5)
  46. assert np.all(a.numpy() == b.numpy())
  47. def test_range_uniform_static_diff_result():
  48. @jit.trace(symbolic=True)
  49. def graph_a():
  50. return R.uniform(5, low=-2, high=2)
  51. @jit.trace(symbolic=True)
  52. def graph_b():
  53. return R.uniform(5, low=-2, high=2)
  54. a = graph_a()
  55. b = graph_b()
  56. assert np.any(a.numpy() != b.numpy())
  57. def test_range_uniform_static_same_result():
  58. @jit.trace(symbolic=True)
  59. def graph_a():
  60. R.manual_seed(731)
  61. return R.uniform(5, low=-2, high=2)
  62. @jit.trace(symbolic=True)
  63. def graph_b():
  64. R.manual_seed(731)
  65. return R.uniform(5, low=-2, high=2)
  66. a = graph_a()
  67. b = graph_b()
  68. assert np.all(a.numpy() == b.numpy())
  69. def test_range_uniform_dynamic_diff_result():
  70. a = R.uniform(5, low=-2, high=2)
  71. b = R.uniform(5, low=-2, high=2)
  72. assert np.any(a.numpy() != b.numpy())
  73. def test_range_uniform_dynamic_same_result():
  74. R.manual_seed(0)
  75. a = R.uniform(5, low=-2, high=2)
  76. R.manual_seed(0)
  77. b = R.uniform(5, low=-2, high=2)
  78. assert np.all(a.numpy() == b.numpy())
  79. def test_dropout_dynamic_diff_result():
  80. x = mge.ones(10)
  81. a = F.dropout(x, 0.5)
  82. b = F.dropout(x, 0.5)
  83. assert np.any(a.numpy() != b.numpy())
  84. def test_dropout_dynamic_same_result():
  85. x = mge.ones(10)
  86. R.manual_seed(0)
  87. a = F.dropout(x, 0.5)
  88. R.manual_seed(0)
  89. b = F.dropout(x, 0.5)
  90. assert np.all(a.numpy() == b.numpy())
  91. def test_M_dropout_static_diff_result():
  92. m = M.Dropout(0.5)
  93. @jit.trace(symbolic=True)
  94. def graph_a(x):
  95. return m(x)
  96. @jit.trace(symbolic=True)
  97. def graph_b(x):
  98. return m(x)
  99. x = np.ones(10, dtype="float32")
  100. a = graph_a(x)
  101. a = a.numpy().copy()
  102. b = graph_b(x)
  103. c = graph_a(x)
  104. assert np.any(a != b.numpy())
  105. assert np.any(a != c.numpy())
  106. def test_M_dropout_static_same_result():
  107. m = M.Dropout(0.5)
  108. @jit.trace(symbolic=True)
  109. def graph_a(x):
  110. return m(x)
  111. @jit.trace(symbolic=True)
  112. def graph_b(x):
  113. return m(x)
  114. x = np.ones(10, dtype="float32")
  115. R.manual_seed(0)
  116. a = graph_a(x)
  117. a = a.numpy().copy()
  118. R.manual_seed(0)
  119. b = graph_b(x)
  120. R.manual_seed(0) # useless
  121. c = graph_a(x)
  122. assert np.all(a == b.numpy())
  123. assert np.any(a != c.numpy())

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