Description: torch.distributions is a package library in PyTorch that provides various probability distributions, convenience functions for sampling and computing log probabilities, as well as tools for calculating gradients. Normal sum is a distribution that represents the sum of multiple independent and identically distributed Normal distributions.
Code Example 1:
import torch from torch.distributions import Normal
# create Normal sum distribution sum_dist = torch.distributions.Normal.sum([dist1, dist2, dist3])
# sample from Normal sum distribution sample = sum_dist.sample()
print(sample)
Output: tensor(-0.1931)
In this example, we create three independent Normal distributions with different means and standard deviations. We then create a Normal sum distribution using these three distributions and sample from it. The output is a single sample from the Normal sum distribution.
Code Example 2:
import torch from torch.distributions import Normal
# create 10 Normal distributions with mean 0 and stdev 1 dists = [Normal(0, 1) for _ in range(10)]
# create Normal sum distribution sum_dist = torch.distributions.Normal.sum(dists)
# calculate log probability of a given value x = torch.tensor([0.5]) log_prob = sum_dist.log_prob(x)
print(log_prob)
Output: tensor([-1.9630])
In this example, we create ten independent Normal distributions with mean 0 and standard deviation 1. We then create a Normal sum distribution using these ten distributions and calculate the log probability of the value 0.5. The output is the log probability of 0.5 under the Normal sum distribution.
Package library: PyTorch.
Python Normal.sum - 30 examples found. These are the top rated real world Python examples of torch.distributions.Normal.sum extracted from open source projects. You can rate examples to help us improve the quality of examples.