PyTorch torch.normal Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.normalis a function in PyTorch used to generate random numbers from a normal distribution.

Function Definition

torch.normal(mean, std, *, generator=None, out=None)
torch.normal(mean, std, size, *, generator=None, out=None)

Parameter Description

  • mean- Mean of the normal distribution
  • std- Standard deviation of the normal distribution
  • size- Shape of the output tensor (optional)
  • generator- Random number generator (optional)
  • out- Output tensor (optional)

Usage Example

Example

import torch

# Generate a random tensor using fixed mean and standard deviation
result1 = torch.normal(mean=0.0, std=1.0, size=(3, 3))
print("Mean 0, standard deviation 1 3x3 normal distribution random tensor:")
print(result1)

# Use tensors as mean and standard deviation
mean = torch.tensor([0.0, 1.0, 2.0])
std = torch.tensor([1.0, 2.0, 3.0])
result2 = torch.normal(mean, std)
print("nSampling result using tensor parameters:")
print(result2)

# Use scalar mean and tensor standard deviation
result3 = torch.normal(mean=0.0, std=torch.tensor([1.0, 2.0, 3.0]))
print("nSampling result with scalar mean and tensor standard deviation:")
print(result3)

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions