.poisson()
Published Feb 28, 2025
Contribute to Docs
The .poisson()
function in PyTorch generates a tensor where each element is drawn from a Poisson distribution with a rate parameter provided in the input tensor. The function is useful for modeling count-based data, such as event occurrences over time.
Syntax
torch.poisson(input, generator=None)
input
: A tensor containing rate parameters for the Poisson distribution. Each value must be non-negative.generator
(Optional): A random number generator for reproducibility. Defaults toNone
.
It returns a tensor with the same shape as input, where each element is a Poisson-distributed random value.
Example
The following example demonstrates how to use .poisson()
:
import torch# Create a tensor containing Poisson rate parametersrates = torch.tensor([2.0, 5.0, 10.0, 3.5])# Generate samples from the Poisson distributionsamples = torch.poisson(rates)# Print the resulting tensorprint(samples)
The code above generates the output as follows:
tensor([4., 6., 7., 6.])
Note: Since
.poisson()
generates random samples, the output values may vary each time the code is run.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours