Programming Language: Python

Namespace/Package Name: torch.distributions

Class/Type: Categorical

Examples at hotexamples.com: 43

The `torch.distributions.Categorical` module in PyTorch is a distribution that can be used to represent discrete probability distributions. This distribution has a single parameter, `probs`, which specifies the probabilities of each possible outcome of a random variable.

Here are some examples of how the `Categorical` distribution can be used in Python code:

**Example 1:**

In this example, a `Categorical` distribution is created with the `probs` parameter set to `[0.25, 0.25, 0.5]`. This means that the distribution has three possible outcomes with probabilities 0.25, 0.25, and 0.5, respectively. A single sample is then drawn from the distribution using the `sample()` method, and the result is printed.

**Example 2:**

In this example, a `Categorical` distribution is created with the `logits` parameter set to `[1.0, 2.0, 3.0]`. The logits are converted to probabilities automatically, and the resulting distribution has three possible outcomes with probabilities 0.09, 0.24, and 0.67, respectively. The `probs` attribute is then used to get the probabilities of each possible outcome, which are printed.

**Example 3:**

Here are some examples of how the `Categorical` distribution can be used in Python code:

import torch probs = torch.tensor([0.25, 0.25, 0.5]) dist = torch.distributions.Categorical(probs=probs) sample = dist.sample() # generates a single sample from the distribution print(sample)

In this example, a `Categorical` distribution is created with the `probs` parameter set to `[0.25, 0.25, 0.5]`. This means that the distribution has three possible outcomes with probabilities 0.25, 0.25, and 0.5, respectively. A single sample is then drawn from the distribution using the `sample()` method, and the result is printed.

import torch logits = torch.tensor([1.0, 2.0, 3.0]) dist = torch.distributions.Categorical(logits=logits) probs = dist.probs # gets the probabilities of each possible outcome print(probs)

In this example, a `Categorical` distribution is created with the `logits` parameter set to `[1.0, 2.0, 3.0]`. The logits are converted to probabilities automatically, and the resulting distribution has three possible outcomes with probabilities 0.09, 0.24, and 0.67, respectively. The `probs` attribute is then used to get the probabilities of each possible outcome, which are printed.

import torch probs = torch.tensor([[0.25, 0.75], [0.5, 0.5], [0.8, 0.2]]) dist = torch.distributions.Categorical(probs=probs) samples = dist.sample((2, 3)) # generates a batch of samples from the distribution print(samples)In this example, a `Categorical` distribution is created with a 3-by-2 tensor of probabilities. This means that the distribution has two possible outcomes for each of three random variables, with probabilities specified by the tensor. The `sample()` method is then used to generate a batch of 2-by-3 samples from the distribution, which are printed. The `torch.distributions` package library contains a collection of probability distributions, such as Bernoulli, Beta, Normal, and so on.

Frequently Used Methods