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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. .. testoutput::
  32. :options: +SKIP
  33. [[-0.20235455 -0.6959438 ]
  34. [-1.4939808 -1.5824696 ]]
  35. """
  36. if size is None:
  37. size = (1,)
  38. seed = _random_seed_generator().__next__()
  39. op = GaussianRNG(seed=seed, mean=mean, std=std)
  40. size = Tensor(size, dtype="int32")
  41. (output,) = apply(op, size)
  42. return output
  43. def uniform(
  44. low: float = 0, high: float = 1, size: Optional[Iterable[int]] = None
  45. ) -> Tensor:
  46. r"""Random variable with uniform distribution $U(0, 1)$
  47. :param size: Output tensor size
  48. :param low: Lower range
  49. :param high: Upper range
  50. :return: The output tensor
  51. Examples:
  52. .. testcode::
  53. import megengine as mge
  54. import megengine.random as rand
  55. x = rand.uniform(size=(2, 2))
  56. print(x.numpy())
  57. .. testoutput::
  58. :options: +SKIP
  59. [[0.76901674 0.70496535]
  60. [0.09365904 0.62957656]]
  61. """
  62. assert low < high, "Uniform is not defined when low >= high"
  63. if size is None:
  64. size = (1,)
  65. seed = _random_seed_generator().__next__()
  66. op = UniformRNG(seed=seed)
  67. size = Tensor(size, dtype="int32")
  68. (output,) = apply(op, size)
  69. return low + (high - low) * output

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