Can someone please explain what torch.multinomial does?

With numpy’s np.random.multinomial it is more or less clear: it take the number of trials (experiments), the probabilities for each outcome (which sum up to 1) and the size of the output (i.e. the number of drawn samples). So for example, if we the experiment is to throw a six-sided fair dice and we do it n=20 times, then

By default torch.multinomial will use replacement=False.
Since only two weights are positive in your example, you won’t be able to draw 4 samples with this setup.
Using torch.multinomial(weights, 4, replacement=True) will work on the other hand.

The returned tensor will give you the samples indices. In your code example you would sample index1 and index2.

It has a different interpretation than the numpy’s result (see my dice example): the pytorch returns shuffled indices (?) which doesn’t make much sense to me.

Also, I would like to understand how the weights w1, w2, … (e.g. weights [0, 10, 3, 0]) translate to probabilities p1, p2, … in multinomial distribution:

torch.multinomial will return the drawn indices, while numpy returns the count of the drawn samples.
To get the count, you could use unique(return_counts=True):

Thanks ptrblck! I also found that torch.distributions.Multinomial does what I want more directly:

m = torch.distributions.Multinomial(20, torch.tensor([1/6]*6))
m.sample()

I am still not sure how to work with weights (not probabilities) though. Using the previous example weights = torch.tensor([0, 10, 3, 0], dtype=torch.float) we have w1 = 0, w2 = 10, w3 = 3, w4 = 0. Are the probabilities p1 = 0, p2 = 10/13, p3 = 3/13, p4 = 0 in this case?