multinomial#

ivy.multinomial(population_size, num_samples, /, *, batch_size=1, probs=None, replace=True, device=None, seed=None, out=None)[source]#

Draws samples from a multinomial distribution. Specifically, returns a tensor where each row contains num_samples indices sampled from the multinomial probability distribution located in the corresponding row of tensor input.

Parameters:
  • population_size (int) – The size of the population from which to draw samples.

  • num_samples (int) – Number of independent samples to draw from the population.

  • batch_size (int, default: 1) – Number of tensors to generate. Default is 1.

  • probs (Optional[Union[Array, NativeArray]], default: None) – The unnormalized probabilities for all elements in population, default is uniform [batch_shape, population_size]

  • replace (bool, default: True) – Whether to replace samples once they’ve been drawn. Default is True.

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None)

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – Drawn samples indices from the multinomial distribution.

Examples

>>> y = ivy.multinomial(10, 5)
>>> print(y)
ivy.array([[1, 8, 7, 8, 3]])
>>> y = ivy.multinomial(10, 5, batch_size=2, seed=42)
>>> print(y)
ivy.array([[3, 9, 7, 5, 1],
       [1, 0, 8, 6, 7]])
>>> y = ivy.multinomial(10, 5, replace=False)
>>> print(y)
ivy.array([[2, 6, 4, 7, 0]])

With ivy.Array input:

>>> y = ivy.multinomial(10, 5, probs=ivy.array([1/10]*10))
>>> print(y)
ivy.array([5, 2, 7, 6, 9])
>>> y = ivy.multinomial(7, 5, batch_size=2, probs=ivy.array([[1/7]*7, [1/7]*7]))
>>> print(y)
ivy.array([[0, 4, 3, 4, 5], [1, 1, 0, 3, 2]])
>>> y = ivy.multinomial(7, 5, batch_size=2, probs=ivy.array([[1/7]*7, [1/7]*7]),
...                     replace=False)
>>> print(y)
ivy.array([[2, 6, 1, 0, 3], [1, 0, 2, 5, 6]])

With ivy.NativeArray input:

>>> y = ivy.multinomial(10, 5, probs=ivy.native_array([1/10]*10))
>>> print(y)
ivy.array([5, 7, 4, 2, 1])
>>> y = ivy.multinomial(10, 5, batch_size=2,
...                     probs=ivy.native_array([[1/10]*10, [1/10]*10]))
>>> print(y)
ivy.array([[8, 0, 4, 1, 7], [2, 3, 4, 9, 3]])
>>> y = ivy.multinomial(10, 5, batch_size=2,
...                     probs=ivy.native_array([[1/10]*10, [1/10]*10]),
...                     replace=False)
>>> print(y)
ivy.array([[0, 2, 6, 9, 1], [6, 7, 2, 4, 3]])
Array.multinomial(self, population_size, num_samples, /, *, batch_size=1, replace=True, device=None, seed=None, out=None)[source]#

ivy.Array instance method variant of ivy.multinomial. This method simply wraps the function, and so the docstring for ivy.multinomial also applies to this method with minimal changes.

Parameters:
  • self (Array) – The unnormalized probabilities for all elements in population, default is uniform [batch_shape, population_size]

  • population_size (int) – The size of the population from which to draw samples.

  • num_samples (int) – Number of independent samples to draw from the population.

  • batch_size (int, default: 1) – Number of tensors to generate. Default is 1.

  • replace (bool, default: True) – Whether to replace samples once they’ve been drawn. Default is True.

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None)

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – Drawn samples from the parameterized normal distribution.

Container.multinomial(self, population_size, num_samples, /, *, batch_size=1, replace=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, device=None, seed=None, out=None)[source]#

ivy.Container instance method variant of ivy.multinomial. This method simply wraps the function, and so the docstring for ivy.multinomial also applies to this method with minimal changes.

Parameters:
  • self (Container) – The unnormalized probabilities for all elements in population, default is uniform [batch_shape, population_size]

  • population_size (Union[int, Container]) – The size of the population from which to draw samples.

  • num_samples (Union[int, Container]) – Number of independent samples to draw from the population.

  • batch_size (Union[int, Container], default: 1) – Number of tensors to generate. Default is 1.

  • replace (Union[bool, Container], default: True) – Whether to replace samples once they’ve been drawn. Default is True.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • device (Optional[Union[Device, NativeDevice, Container]], default: None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None)

  • seed (Optional[Union[int, Container]], default: None) – A python integer. Used to create a random seed distribution

  • out (Optional[Container], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – Drawn samples from the parameterized normal distribution.