【问题标题】:Is torch.empty_like() dependent on the input value as well as input size?torch.empty_like() 是否取决于输入值和输入大小?
【发布时间】:2021-05-26 01:11:40
【问题描述】:

torch.empty_like 的火炬文档中的描述说:

torch.empty_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → 张量

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

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

我要做的是:

>>> 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_like 返回的张量取决于输入值,这与文档中的描述相反。有人可以解释一下吗?

【问题讨论】:

  • 未初始化内存的内容可以是任何东西。 .empty_like() 仅在您将立即为所有元素分配一些值的情况下才有用。

标签: python pytorch


【解决方案1】:

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

注意torch.empty_like 确实取决于输入的dtype(但不是它的具体值)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多