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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. import megengine._internal as mgb
  11. from megengine._internal import CompGraph, CompNode
  12. from ..core.graph import _use_default_if_none
  13. from ..core.tensor import Tensor, wrap_io_tensor
  14. from .rng import _random_seed_generator
  15. __all__ = ["gaussian", "uniform"]
  16. @wrap_io_tensor
  17. def gaussian(
  18. shape: Iterable[int],
  19. mean: float = 0,
  20. std: float = 1,
  21. comp_node: Optional[CompNode] = None,
  22. comp_graph: Optional[CompGraph] = None,
  23. ) -> Tensor:
  24. r"""Random variable with Gaussian distribution $N(\mu, \sigma)$
  25. :param shape: Output tensor shape
  26. :param mean: The mean or expectation of the distribution
  27. :param std: The standard deviation of the distribution (variance = $\sigma ^ 2$)
  28. :param comp_node: The comp node output on, default to None
  29. :param comp_graph: The graph in which output is, default to None
  30. :return: The output tensor
  31. Examples:
  32. .. testcode::
  33. import megengine as mge
  34. import megengine.random as rand
  35. x = rand.gaussian((2, 2), mean=0, std=1)
  36. print(x.numpy())
  37. .. testoutput::
  38. :options: +SKIP
  39. [[-0.20235455 -0.6959438 ]
  40. [-1.4939808 -1.5824696 ]]
  41. """
  42. comp_node, comp_graph = _use_default_if_none(comp_node, comp_graph)
  43. seed = _random_seed_generator().__next__()
  44. return mgb.opr.gaussian_rng(
  45. shape, seed=seed, mean=mean, std=std, comp_node=comp_node, comp_graph=comp_graph
  46. )
  47. @wrap_io_tensor
  48. def uniform(
  49. shape: Iterable[int],
  50. low: float = 0,
  51. high: float = 1,
  52. comp_node: Optional[CompNode] = None,
  53. comp_graph: Optional[CompGraph] = None,
  54. ) -> Tensor:
  55. r"""Random variable with uniform distribution $U(0, 1)$
  56. :param shape: Output tensor shape
  57. :param low: Lower range
  58. :param high: Upper range
  59. :param comp_node: The comp node output on, default to None
  60. :param comp_graph: The graph in which output is, default to None
  61. :return: The output tensor
  62. Examples:
  63. .. testcode::
  64. import megengine as mge
  65. import megengine.random as rand
  66. x = rand.uniform((2, 2))
  67. print(x.numpy())
  68. .. testoutput::
  69. :options: +SKIP
  70. [[0.76901674 0.70496535]
  71. [0.09365904 0.62957656]]
  72. """
  73. comp_node, comp_graph = _use_default_if_none(comp_node, comp_graph)
  74. seed = _random_seed_generator().__next__()
  75. return low + (high - low) * mgb.opr.uniform_rng(
  76. shape, seed=seed, comp_node=comp_node, comp_graph=comp_graph
  77. )

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