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

PyTorch torch.ones_like function

PyTorch torch.ones_like function


PyTorch torch reference manual PyTorch torch reference manual

torch.ones_like is a function in PyTorch used to create a tensor of ones with the same shape as the input tensor.

Function definition

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


Usage examples

Example

import

torch

x

=

torch.

randn

(

2

,

3

)

y

=

torch.

ones_like

(

x

)

print

(

y

)

The output is:

tensor([[1., 1., 1.],

        [1., 1., 1.]])


PyTorch torch reference manual PyTorch torch reference manual