PyTorch torch.normal function
PyTorch torch reference manual
torch.normal is a function in PyTorch that generates 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 distributionstd- Standard deviation of the normal distributionsize- Shape of the output tensor (optional)generator- Random number generator (optional)out- Output tensor (optional)
Usage examples
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
)
)
(
"3x3 random tensor from a normal distribution with mean 0 and std 1:"
)
(
result1
)
Use tensors as the 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
)
(
"nSampling result using tensor parameters:"
)
(
result2
)
Use a scalar mean and tensor standard deviation
result3
=
torch.
normal
(
mean
=
0.0
,
std
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
)
)
(
"nSampling result with scalar mean and tensor std:"
)
(
result3
)