PyTorch torch.ones_like function
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
)
(
y
)
The output is:
tensor([[1., 1., 1.],
[1., 1., 1.]])