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

PyTorch torch.rand_like function

PyTorch torch.rand_like function


PyTorch torch reference manual PyTorch torch reference manual

torch.rand_like is a function in PyTorch used to create a uniformly distributed random tensor with the same shape as the input tensor.

Function definition

torch.rand_like(input, dtype=None, device=None, requires_grad=False)


Usage examples

Example

import

torch

Create an input tensor

input

=

torch.

tensor

(

[

[

1

,

2

]

,

[

3

,

4

]

]

)

Create a uniformly distributed random tensor [0, 1) with the same shape as the input

x

=

torch.

rand_like

(

input

)

print

(

x

)


PyTorch torch reference manual PyTorch torch reference manual