Python torch.empty_like()是否取决于输入值和输入大小?

Python torch.empty_like()是否取决于输入值和输入大小?,python,pytorch,Python,Pytorch,torch文档中的描述表示: torch.empty_like(输入,*,数据类型=None,布局=None,设备=None,需要\u梯度=False,内存\u格式=torch.preserve\u格式)→ 张量 返回与输入大小相同的未初始化张量torch.empty_like(input)相当于torch.empty(input.size(),dtype=input.dtype,layout=input.layout,device=input.device) 参数 输入(张量)–输入的大小将决

torch文档中的描述表示:

torch.empty_like(输入,*,数据类型=None,布局=None,设备=None,需要\u梯度=False,内存\u格式=torch.preserve\u格式)→ 张量

返回与输入大小相同的未初始化张量
torch.empty_like(input)
相当于
torch.empty(input.size(),dtype=input.dtype,layout=input.layout,device=input.device)

参数
输入(张量)–输入的大小将决定输出张量的大小

我所做的是:

>>> torch.empty(3,4)
tensor([[-1.8597e+15,  4.5657e-41, -1.8597e+15,  4.5657e-41],
        [ 4.4842e-44,  0.0000e+00,  8.9683e-44,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00]])

>>> c1
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])

>>> torch.empty_like(c1)
tensor([[139942262173040,  93851872482144,               1,               0],
        [              0,               0,  93851872492496,               0],
        [              0,               0,               0,               0]])

>>> d
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])

>>> torch.empty_like(d)
tensor([[-8.6092e-25,  3.0620e-41,  0.0000e+00,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00]])

与文档中的描述相反,
torch.empty\u like
返回的张量似乎取决于输入值。有人能解释一下吗?

文档描述是正确的。我不确定您是否对
torch.empty感到困惑,比如在不同的调用中返回不同的输出,但您可以看到这也是
torch.empty的行为,例如多次调用
torch.empty((2,3),dtype=torch.int64)


torch.empty_like
取决于输入的
dtype
(但不是其特定值)。

未初始化内存的内容可以是任何内容
.empty_like()
仅在您将立即为所有元素赋值的情况下有用。