PyTorch torch.ones_like Function


Pytorch torch 参考手册Pytorch torch Reference Manual

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

Function Definition

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

Usage Example

Example

import torch

x = torch.randn(2, 3)
y = torch.ones_like(x)

print(y)

The output result is:

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

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions