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

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