【问题标题】:Gradient error when calculating - pytorch计算时的梯度误差 - pytorch
【发布时间】:2018-11-17 21:56:09
【问题描述】:

我正在学习使用 pytorch (0.4.0) 来自动计算梯度,但是我不太明白如何使用后向 () 和 grad,因为我正在做一个需要计算 df / dw 的练习使用 pytorch 和 解析求导,分别返回auto_grad,user_grad,但是我不太明白自动微分的使用,代码中我做了f.backward(),做了w.grad求df/dw,另外两个计算是不对应,如果我什至弄错了导数,它会遵循我正在使用的图表和我正在尝试做的代码:

import numpy as np
import torch
import torch.nn.functional as F

def graph2(W_np, x_np, b_np):
    W = torch.Tensor(W_np)
    W.requires_grad = True
    x = torch.Tensor(x_np)
    b = torch.Tensor(b_np)
    u = torch.matmul(W, x) + b
    g = F.sigmoid(u)
    f = torch.sum(g)
    user_grad = (sigmoid(W_np*x_np + b_np)*(1 - sigmoid(W_np*x_np + b_np))).T*x_np
    f.backward(retain_graph=True)
    auto_grad = W.grad

    print(auto_grad) 
    print(user_grad)



 #   raise NotImplementedError("falta completar a função graph2")
    # END YOUR CODE
    return f, auto_grad, user_grad

测试:

iterations = 1000
sizes = np.random.randint(2,10, size=(iterations))
for i in range(iterations):
    size = sizes[i]
    W_np = np.random.rand(size, size)
    x_np = np.random.rand(size, 1)
    b_np = np.random.rand(size, 1)
    f, auto_grad, user_grad = graph2(W_np, x_np, b_np)
    manual_f = np.sum(sigmoid(np.matmul(W_np, x_np) + b_np))
    assert np.isclose(f.data.numpy(), manual_f, atol=1e-4), "f not correct"
    assert np.allclose(auto_grad.numpy(), user_grad), "Gradient not correct"

【问题讨论】:

    标签: numpy graph neural-network pytorch


    【解决方案1】:

    我认为您以错误的方式计算梯度。试试这个。

    import numpy as np
    import torch
    from torch.autograd import Variable
    import torch.nn.functional as F
    
    def sigmoid(x):
        return 1.0 / (1.0 + np.exp(-x))
    
    def graph2(W_np, x_np, b_np):
        W = Variable(torch.Tensor(W_np), requires_grad=True)
        x = torch.tensor(x_np, requires_grad=True).type(torch.FloatTensor)
        b = torch.tensor(b_np, requires_grad=True).type(torch.FloatTensor)
        u = torch.matmul(W, x) + b
        g = F.sigmoid(u)
        f = torch.sum(g)
        user_grad = (sigmoid(np.matmul(W_np, x_np) + b_np)*(1 - sigmoid(np.matmul(W_np, x_np) + b_np)))*x_np.T
        f.backward(retain_graph=True)
        auto_grad = W.grad
        print("auto_grad", auto_grad) 
        print("user_grad", user_grad)
        # END YOUR CODE
        return f, auto_grad, user_grad
    
    
    
    
    iterations = 1000
    sizes = np.random.randint(2,10, size=(iterations))
    for i in range(iterations):
        size = sizes[i]
        print("i, size", i, size)
        W_np = np.random.rand(size, size)
        x_np = np.random.rand(size, 1)
        b_np = np.random.rand(size, 1)
        f, auto_grad, user_grad = graph2(W_np, x_np, b_np)
        manual_f = np.sum(sigmoid(np.matmul(W_np, x_np) + b_np))
        assert np.isclose(f.data.numpy(), manual_f, atol=1e-4), "f not correct"
        assert np.allclose(auto_grad.numpy(), user_grad), "Gradient not correct"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      相关资源
      最近更新 更多