Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.normal function

PyTorch torch.normal function


PyTorch torch reference manual 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


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

)

)

print

(

"3x3 random tensor from a normal distribution with mean 0 and std 1:"

)

print

(

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

)

print

(

"nSampling result using tensor parameters:"

)

print

(

result2

)

Use a 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 std:"

)

print

(

result3

)


PyTorch torch reference manual PyTorch torch reference manual