Learn practical skills, build real-world projects, and advance your career

All About Distributions in PyTorch

The galore of distributions you can initialize your tensors with...

Distributions are instrumental in making initializations for layers of neural networks. Let's see what PyTorch has to offer when it comes to distributions.

  • torch.Tensor.bernoulli_()
  • torch.Tensor.cauchy_()
  • torch.Tensor.exponential_()
  • torch.Tensor.geometric_()
  • torch.Tensor.log_normal_()
  • torch.Tensor.normal_()
  • torch.Tensor.random_()
  • torch.Tensor.uniform_()
# Import torch and other required modules
import torch

Bernoulli Distribution

First things first, let's flip some coins.

torch.empty(3, 3).bernoulli_()
tensor([[1., 0., 1.],
        [1., 1., 0.],
        [1., 0., 0.]])

We can generate a Tensor with values sampled from Bernoulli distribution for any shape we desire. Here, we create 2-D tensor to demonstrate that.