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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.ops import get_global_rng_seed as _get_global_rng_seed
  12. from .rng import _normal, _uniform
  13. __all__ = ["normal", "uniform"]
  14. def normal(
  15. mean: float = 0, std: float = 1, size: Optional[Iterable[int]] = None
  16. ) -> Tensor:
  17. r"""
  18. Random variable with Gaussian distribution :math:`N(\mu, \sigma)`.
  19. :param size: output tensor size.
  20. :param mean: the mean or expectation of the distribution.
  21. :param std: the standard deviation of the distribution (variance = :math:`\sigma ^ 2`).
  22. :return: the output tensor.
  23. Examples:
  24. .. testcode::
  25. import megengine as mge
  26. import megengine.random as rand
  27. x = rand.normal(mean=0, std=1, size=(2, 2))
  28. print(x.numpy())
  29. Outputs:
  30. .. testoutput::
  31. :options: +SKIP
  32. [[-0.20235455 -0.6959438 ]
  33. [-1.4939808 -1.5824696 ]]
  34. """
  35. return _normal(
  36. mean=mean,
  37. std=std,
  38. size=size,
  39. seed=_get_global_rng_seed(),
  40. device=None,
  41. handle=0,
  42. )
  43. def uniform(
  44. low: float = 0, high: float = 1, size: Optional[Iterable[int]] = None
  45. ) -> Tensor:
  46. r"""
  47. 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. return _uniform(
  65. low=low,
  66. high=high,
  67. size=size,
  68. seed=_get_global_rng_seed(),
  69. device=None,
  70. handle=0,
  71. )

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