# Multinomial

Source:`R/gen-namespace-docs.R`

, `R/gen-namespace-examples.R`

, `R/wrapers.R`

`torch_multinomial.Rd`

Multinomial

## Arguments

- self
(Tensor) the input tensor containing probabilities

- num_samples
(int) number of samples to draw

- replacement
(bool, optional) whether to draw with replacement or not

- generator
(

`torch.Generator`

, optional) a pseudorandom number generator for sampling

## Note

```
`input` do not need to sum to one (in which case we use
The rows of -negative, finite and have
the values as weights), but must be non-zero sum. a non
```

Indices are ordered from left to right according to when each was sampled (first samples are placed in first column).

If `input`

is a vector, `out`

is a vector of size `num_samples`

.

If `input`

is a matrix with `m`

rows, `out`

is an matrix of shape
\((m \times \mbox{num\_samples})\).

If replacement is `TRUE`

, samples are drawn with replacement.

If not, they are drawn without replacement, which means that when a sample index is drawn for a row, it cannot be drawn again for that row.

```
`num_samples` must be lower than
When drawn without replacement, -zero elements in `input` (or the min number of non-zero
number of nonin each row of `input` if it is a matrix). elements
```

## multinomial(input, num_samples, replacement=False, *, generator=NULL, out=NULL) -> LongTensor

Returns a tensor where each row contains `num_samples`

indices sampled
from the multinomial probability distribution located in the corresponding row
of tensor `input`

.

## Examples

```
if (torch_is_installed()) {
weights = torch_tensor(c(0, 10, 3, 0), dtype=torch_float()) # create a tensor of weights
torch_multinomial(weights, 2)
torch_multinomial(weights, 4, replacement=TRUE)
}
#> torch_tensor
#> 2
#> 3
#> 2
#> 3
#> [ CPULongType{4} ]
```