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.

distribution.py 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 typing import Iterable, Optional
  10. from .. import Tensor
  11. from ..core._imperative_rt import invoke_op
  12. from ..core.ops.builtin import GaussianRNG, UniformRNG
  13. from ..core.tensor import utils
  14. from ..core.tensor.core import apply
  15. from .rng import _random_seed_generator
  16. __all__ = ["normal", "uniform"]
  17. def normal(
  18. mean: float = 0, std: float = 1, size: Optional[Iterable[int]] = None
  19. ) -> Tensor:
  20. r"""Random variable with Gaussian distribution $N(\mu, \sigma)$
  21. :param size: Output tensor size
  22. :param mean: The mean or expectation of the distribution
  23. :param std: The standard deviation of the distribution (variance = $\sigma ^ 2$)
  24. :return: The output tensor
  25. Examples:
  26. .. testcode::
  27. import megengine as mge
  28. import megengine.random as rand
  29. x = rand.normal(mean=0, std=1, size=(2, 2))
  30. print(x.numpy())
  31. Outputs:
  32. .. testoutput::
  33. :options: +SKIP
  34. [[-0.20235455 -0.6959438 ]
  35. [-1.4939808 -1.5824696 ]]
  36. """
  37. if size is None:
  38. size = (1,)
  39. seed = _random_seed_generator().__next__()
  40. op = GaussianRNG(seed=seed, mean=mean, std=std)
  41. size = Tensor(size, dtype="int32")
  42. (output,) = apply(op, size)
  43. return output
  44. def uniform(
  45. low: float = 0, high: float = 1, size: Optional[Iterable[int]] = None
  46. ) -> Tensor:
  47. r"""Random variable with uniform distribution $U(0, 1)$
  48. :param size: Output tensor size
  49. :param low: Lower range
  50. :param high: Upper range
  51. :return: The output tensor
  52. Examples:
  53. .. testcode::
  54. import megengine as mge
  55. import megengine.random as rand
  56. x = rand.uniform(size=(2, 2))
  57. print(x.numpy())
  58. Outputs:
  59. .. testoutput::
  60. :options: +SKIP
  61. [[0.76901674 0.70496535]
  62. [0.09365904 0.62957656]]
  63. """
  64. assert low < high, "Uniform is not defined when low >= high"
  65. if size is None:
  66. size = (1,)
  67. seed = _random_seed_generator().__next__()
  68. op = UniformRNG(seed=seed)
  69. size = Tensor(size, dtype="int32")
  70. (output,) = apply(op, size)
  71. return low + (high - low) * output

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