代码之家  ›  专栏  ›  技术社区  ›  Juan Leni

Pythorch教程中有错误吗?

  •  0
  • Juan Leni  · 技术社区  · 6 年前

    官方pytorch教程( https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html#gradients out.backward() out.backward(torch.tensor(1)) 相当于。但事实似乎并非如此。

    import torch
    
    x = torch.ones(2, 2, requires_grad=True)
    y = x + 2
    z = y * y * 3
    out = z.mean()
    
    # option 1    
    out.backward()
    
    # option 2. Replace! do not leave one after the other
    # out.backward(torch.tensor(1))
    
    print(x.grad)
    

    注意 :不要留下两个反向呼叫。将选项1替换为2。

    更新 如果我用 正如教程所说,我得到:

    E       RuntimeError: invalid gradient at index 0 - expected type torch.FloatTensor but got torch.LongTensor
    
    ../../../anaconda3/envs/phd/lib/python3.6/site-packages/torch/autograd/__init__.py:90: RuntimeError
    

    我也试过用 out.backward(torch.Tensor(1)) 我得到的是:

    E       RuntimeError: invalid gradient at index 0 - expected shape [] but got [1]
    
    ../../../anaconda3/envs/phd/lib/python3.6/site-packages/torch/autograd/__init__.py:90: RuntimeError
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   MBT Nina Golyandina    6 年前

    你需要使用 dtype=torch.float :

    import torch
    
    x = torch.ones(2, 2, requires_grad=True)
    y = x + 2
    z = y * y * 3
    out = z.mean()
    
    # option 1    
    out.backward()
    print(x.grad)
    
    
    x = torch.ones(2, 2, requires_grad=True)
    y = x + 2
    z = y * y * 3
    out = z.mean()
    
    
    
    #option 2. Replace! do not leave one after the other
    out.backward(torch.tensor(1, dtype=torch.float))
    
    print(x.grad)
    

    tensor([[ 4.5000,  4.5000],
            [ 4.5000,  4.5000]])
    tensor([[ 4.5000,  4.5000],
            [ 4.5000,  4.5000]])