plpm
plpm

Reputation: 599

Can someone please explain about the functionality of "torch.bernoulli"?

I have a question about torch.bernoulli. According to the documentation:

Draws binary random numbers (0 or 1) from a Bernoulli distribution. The input tensor should be a tensor containing probabilities to be used for drawing the binary random number. Hence, all values in input have to be in the range: 0 ≤ input i ≤ 1. The i-th element of the output tensor will draw a value 1 according to i-th probability value given in input.

I don't quite get it. For example, if we have torch.bernoulli([0.599, 0.0846, 0.0179, 0.0742, 0.0742]), once it returns tensor([1., 0., 0., 0., 0.]). Another time it returns tensor([0., 0., 0., 1., 0.]). I don't understand why it behaves like that. The documentation states "...for drawing the binary random number...". But the whole proccess is vague to me. Can someone explain please?

Upvotes: 1

Views: 782

Answers (1)

Prune
Prune

Reputation: 77885

Let's simplify your example to the input tensor [0.60, 0.07, 0.12]. Now, on any given draw, there is a ...

60% chance that element 1 will be 1
 7% chance that element 2 will be 1
12% chance that element 3 will be 1
... and any elements that aren't 1 are 0.

These events are independent. For instance, there is a chance of 0.60 * 0.07 * 0.12 that all three elements will be 1. There is a 0.40 * 0.93 * 0.88 chance that all three will be 0.

Does that help clear it up?

Upvotes: 3

Related Questions