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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 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._imperative_rt.core2 import apply
  13. from ..core.ops.builtin import GaussianRNG, UniformRNG
  14. from ..core.tensor import utils
  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"""
  21. Random variable with Gaussian distribution :math:`N(\mu, \sigma)`.
  22. :param size: output tensor size.
  23. :param mean: the mean or expectation of the distribution.
  24. :param std: the standard deviation of the distribution (variance = :math:`\sigma ^ 2`).
  25. :return: the output tensor.
  26. Examples:
  27. .. testcode::
  28. import megengine as mge
  29. import megengine.random as rand
  30. x = rand.normal(mean=0, std=1, size=(2, 2))
  31. print(x.numpy())
  32. Outputs:
  33. .. testoutput::
  34. :options: +SKIP
  35. [[-0.20235455 -0.6959438 ]
  36. [-1.4939808 -1.5824696 ]]
  37. """
  38. if size is None:
  39. size = (1,)
  40. op = GaussianRNG(mean, std)
  41. _ref = Tensor([], dtype="int32")
  42. shape = utils.astensor1d(size, _ref, dtype="int32")
  43. shape = Tensor(shape, dtype="int32")
  44. (output,) = apply(op, shape)
  45. return output
  46. def uniform(
  47. low: float = 0, high: float = 1, size: Optional[Iterable[int]] = None
  48. ) -> Tensor:
  49. r"""
  50. Random variable with uniform distribution $U(0, 1)$.
  51. :param size: output tensor size.
  52. :param low: lower range.
  53. :param high: upper range.
  54. :return: the output tensor.
  55. Examples:
  56. .. testcode::
  57. import megengine as mge
  58. import megengine.random as rand
  59. x = rand.uniform(size=(2, 2))
  60. print(x.numpy())
  61. Outputs:
  62. .. testoutput::
  63. :options: +SKIP
  64. [[0.76901674 0.70496535]
  65. [0.09365904 0.62957656]]
  66. """
  67. assert low < high, "Uniform is not defined when low >= high"
  68. if size is None:
  69. size = (1,)
  70. op = UniformRNG()
  71. _ref = Tensor([], dtype="int32")
  72. shape = utils.astensor1d(size, _ref, dtype="int32")
  73. shape = Tensor(shape, dtype="int32")
  74. (output,) = apply(op, shape)
  75. return low + (high - low) * output

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