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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. [[ 0.2925366 -0.718359 ]
  39. [ 0.09999694 -0.3931978 ]]
  40. """
  41. comp_node, comp_graph = _use_default_if_none(comp_node, comp_graph)
  42. seed = _random_seed_generator().__next__()
  43. return mgb.opr.gaussian_rng(
  44. shape, seed=seed, mean=mean, std=std, comp_node=comp_node, comp_graph=comp_graph
  45. )
  46. @wrap_io_tensor
  47. def uniform(
  48. shape: Iterable[int],
  49. comp_node: Optional[CompNode] = None,
  50. comp_graph: Optional[CompGraph] = None,
  51. ) -> Tensor:
  52. r"""Random variable with uniform distribution $U(0, 1)$
  53. :param shape: Output tensor shape
  54. :param comp_node: The comp node output on, default to None
  55. :param comp_graph: The graph in which output is, default to None
  56. :return: The output tensor
  57. Examples:
  58. .. testcode::
  59. import megengine as mge
  60. import megengine.random as rand
  61. x = rand.uniform((2, 2))
  62. print(x.numpy())
  63. .. testoutput::
  64. [[0.74021935 0.9209938 ]
  65. [0.03902049 0.9689629 ]]
  66. """
  67. comp_node, comp_graph = _use_default_if_none(comp_node, comp_graph)
  68. seed = _random_seed_generator().__next__()
  69. return mgb.opr.uniform_rng(
  70. shape, seed=seed, comp_node=comp_node, comp_graph=comp_graph
  71. )

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

Contributors (1)