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.

mindspore.nn.probability.distribution.Bernoulli.rst 4.4 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. mindspore.nn.probability.distribution.Bernoulli
  2. ================================================
  3. .. py:class:: mindspore.nn.probability.distribution.Bernoulli(probs=None, seed=None, dtype=mstype.int32, name='Bernoulli')
  4. 伯努利分布(Bernoulli Distribution)。
  5. 离散随机分布,取值范围为 :math:`\{0, 1\}` ,概率质量函数为 :math:`P(X = 0) = p, P(X = 1) = 1-p`。
  6. **参数:**
  7. - **probs** (float, list, numpy.ndarray, Tensor) - 结果是1的概率。默认值:None。
  8. - **seed** (int) - 采样时使用的种子。如果为None,则使用全局种子。默认值:None。
  9. - **dtype** (mindspore.dtype) - 采样结果的数据类型。默认值:mindspore.int32.
  10. - **name** (str) - 分布的名称。默认值:'Bernoulli'。
  11. **支持平台:**
  12. ``Ascend`` ``GPU``
  13. .. note::
  14. `probs` 中元素必须是合适的概率(0<p<1)。
  15. **异常:**
  16. - **ValueError** - `probs` 中元素小于0或大于1。
  17. - **TypeError** - `dtype` 不是float的子类。
  18. **样例:**
  19. >>> import mindspore
  20. >>> import mindspore.nn as nn
  21. >>> import mindspore.nn.probability.distribution as msd
  22. >>> from mindspore import Tensor
  23. >>> # 初始化伯努利分布,probs设置为1。
  24. >>> b1 = msd.Bernoulli(0.5, dtype=mindspore.int32)
  25. >>> # 伯努利分布可以在没有参数的情况下初始化。
  26. >>> # 在这种情况下,`probs`必须在函数调用过程中通过参数传入。
  27. >>> b2 = msd.Bernoulli(dtype=mindspore.int32)
  28. >>> # 下面是用于测试的Tensor
  29. >>> value = Tensor([1, 0, 1], dtype=mindspore.int32)
  30. >>> probs_a = Tensor([0.6], dtype=mindspore.float32)
  31. >>> probs_b = Tensor([0.2, 0.3, 0.4], dtype=mindspore.float32)
  32. >>>
  33. >>> # 公共接口对应的概率函数的私有接口,包括`prob`、`log_prob`、`cdf`、`log_cdf`、`survival_function`、`log_survival`,它们具有相同的参数,如下所示。
  34. >>> # 参数:
  35. >>> # value (Tensor):要评估的值。
  36. >>> # probs1 (Tensor):成功的概率。默认值:self.probs.
  37. >>> # 下面是调用`prob`的示例(通过将`prob`替换为函数的名称,可以对其他概率函数进行类似的调用):
  38. >>> ans = b1.prob(value)
  39. >>> print(ans.shape)
  40. (3,)
  41. >>> # 评估关于分布b的`prob`。
  42. >>> ans = b1.prob(value, probs_b)
  43. >>> print(ans.shape)
  44. (3,)
  45. >>> # `probs`必须在函数调用期间传入。
  46. >>> ans = b2.prob(value, probs_a)
  47. >>> print(ans.shape)
  48. (3,)
  49. >>>
  50. >>> # 函数`mean`、`sd`、`var`和`entropy`具有相同的参数。
  51. >>> # 参数:
  52. >>> # probs1 (Tensor):成功的概率。默认值:self.probs.
  53. >>> # 下面是调用`mean的`示例。`sd`、`var`和`entropy`与`mean`类似。
  54. >>> ans = b1.mean() # return 0.5
  55. >>> print(ans.shape)
  56. ()
  57. >>> ans = b1.mean(probs_b) # return probs_b
  58. >>> print(ans.shape)
  59. (3,)
  60. >>> # `probs`必须在函数调用期间传入。
  61. >>> ans = b2.mean(probs_a)
  62. >>> print(ans.shape)
  63. (1,)
  64. >>>
  65. >>> # `kl_loss`和`cross_entropy`的接口如下:
  66. >>> # 参数:
  67. >>> # dist (str):分布的名称。仅支持'Bernoulli'。
  68. >>> # probs1_b (Tensor):分布b成功的概率。
  69. >>> # probs1_a (Tensor):分布a成功的概率。默认值:self.probs.
  70. >>> # 下面是调用kl_loss的示例。`cross_entropy`也类似。
  71. >>> ans = b1.kl_loss('Bernoulli', probs_b)
  72. >>> print(ans.shape)
  73. (3,)
  74. >>> ans = b1.kl_loss('Bernoulli', probs_b, probs_a)
  75. >>> print(ans.shape)
  76. (3,)
  77. >>> # 必须传入额外的`probs_a`。
  78. >>> ans = b2.kl_loss('Bernoulli', probs_b, probs_a)
  79. >>> print(ans.shape)
  80. (3,)
  81. >>>
  82. >>> # `sample`示例。
  83. >>> # 参数:
  84. >>> # shape (tuple):样本的shape。默认值:()。
  85. >>> # probs1 (Tensor):成功的概率。默认值:self.probs.
  86. >>> ans = b1.sample()
  87. >>> print(ans.shape)
  88. ()
  89. >>> ans = b1.sample((2,3))
  90. >>> print(ans.shape)
  91. (2, 3)
  92. >>> ans = b1.sample((2,3), probs_b)
  93. >>> print(ans.shape)
  94. (2, 3, 3)
  95. >>> ans = b2.sample((2,3), probs_a)
  96. >>> print(ans.shape)
  97. (2, 3, 1)
  98. .. py:method:: probs
  99. 返回结果为1的概率。
  100. **返回:**
  101. Tensor, 结果为1的概率。