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 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_dropout_dynamic_diff_result():
  48. x = mge.ones(10)
  49. a = F.dropout(x, 0.5)
  50. b = F.dropout(x, 0.5)
  51. assert np.any(a.numpy() != b.numpy())
  52. def test_dropout_dynamic_same_result():
  53. x = mge.ones(10)
  54. R.manual_seed(0)
  55. a = F.dropout(x, 0.5)
  56. R.manual_seed(0)
  57. b = F.dropout(x, 0.5)
  58. assert np.all(a.numpy() == b.numpy())
  59. def test_M_dropout_static_diff_result():
  60. m = M.Dropout(0.5)
  61. @jit.trace(symbolic=True)
  62. def graph_a(x):
  63. return m(x)
  64. @jit.trace(symbolic=True)
  65. def graph_b(x):
  66. return m(x)
  67. x = np.ones(10, dtype="float32")
  68. a = graph_a(x)
  69. a = a.numpy().copy()
  70. b = graph_b(x)
  71. c = graph_a(x)
  72. assert np.any(a != b.numpy())
  73. assert np.any(a != c.numpy())
  74. def test_M_dropout_static_same_result():
  75. m = M.Dropout(0.5)
  76. @jit.trace(symbolic=True)
  77. def graph_a(x):
  78. return m(x)
  79. @jit.trace(symbolic=True)
  80. def graph_b(x):
  81. return m(x)
  82. x = np.ones(10, dtype="float32")
  83. R.manual_seed(0)
  84. a = graph_a(x)
  85. a = a.numpy().copy()
  86. R.manual_seed(0)
  87. b = graph_b(x)
  88. R.manual_seed(0) # useless
  89. c = graph_a(x)
  90. assert np.all(a == b.numpy())
  91. assert np.any(a != c.numpy())

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