TUTORIALAI & dataFETCHED
PyTorch torch.ones_like 函数
PyTorch torch.ones_like 函数 Pytorch torch 参考手册 torch.ones_like 是 PyTorch 中用于创建与输入张量形状相同的全一张量的函数。 函数定义 torch.ones_like(input, dtype=None, device=None, requires_grad=False) 使用示例 实例 [mycode4 type='python'..
Updated 7/16/2026
Tutorial detail pages default to noindex.
PyTorch torch.ones_like 函数
torch.ones_like 是 PyTorch 中用于创建与输入张量形状相同的全一张量的函数。
函数定义
torch.ones_like(input, dtype=None, device=None, requires_grad=False)
使用示例
实例
import torch
x = torch.randn(2, 3)
y = torch.ones_like(x)
print(y)
x = torch.randn(2, 3)
y = torch.ones_like(x)
print(y)
输出结果为:
tensor([[1., 1., 1.],
[1., 1., 1.]])
Pytorch torch 参考手册