Python 需要帮助理解pytorch中的梯度函数吗

Python 需要帮助理解pytorch中的梯度函数吗,python,machine-learning,pytorch,gradient,Python,Machine Learning,Pytorch,Gradient,下面的代码 w = np.array([[2., 2.],[2., 2.]]) x = np.array([[3., 3.],[3., 3.]]) b = np.array([[4., 4.],[4., 4.]]) w = torch.tensor(w, requires_grad=True) x = torch.tensor(x, requires_grad=True) b = torch.tensor(b, requires_grad=True) y = w*x + b print(

下面的代码


w = np.array([[2., 2.],[2., 2.]])
x = np.array([[3., 3.],[3., 3.]])
b = np.array([[4., 4.],[4., 4.]])
w = torch.tensor(w, requires_grad=True)
x = torch.tensor(x, requires_grad=True)
b = torch.tensor(b, requires_grad=True)


y = w*x + b 
print(y)
# tensor([[10., 10.],
#         [10., 10.]], dtype=torch.float64, grad_fn=<AddBackward0>)

y.backward(torch.FloatTensor([[1, 1],[ 1, 1]]))

print(w.grad)
# tensor([[3., 3.],
#         [3., 3.]], dtype=torch.float64)

print(x.grad)
# tensor([[2., 2.],
#         [2., 2.]], dtype=torch.float64)

print(b.grad)
# tensor([[1., 1.],
#         [1., 1.]], dtype=torch.float64)


w=np.数组([[2,2.],[2,2.]]
x=np.数组([[3,3.],[3,3.]]
b=np.数组([[4,4.],[4,4.]))
w=火炬张量(w,需要梯度=真)
x=火炬张量(x,需要梯度=真)
b=火炬张量(b,要求梯度=真)
y=w*x+b
打印(y)
#张量([[10,10.],
#[10,10.],dtype=torch.float64,grad_fn=)
y、 向后(torch.FloatTensor([[1,1],[1,1]]))
打印(w.grad)
#张量([[3,3.],
#[3,3.],dtype=torch.float64)
打印(x.grad)
#张量([[2,2.],
#[2,2.]],dtype=torch.float64)
印刷(学士学位)
#张量([[1,1.],
#[1,1.]],dtype=torch.float64)
由于
gradient
函数中的张量参数是输入张量形式的全一张量,我的理解是

  • w.grad
    表示
    y
    w.r.t
    w
    的导数,并产生
    b

  • x.grad
    表示
    y
    w.r.t
    x
    的导数,并产生
    b

  • b.grad
    表示
    y
    w.r.t
    b
    的导数,并产生所有的导数


  • 其中,只有第三点的答案符合我的预期结果。有人能帮我理解前两个答案吗。我想我理解累加部分,但我不认为这里会发生这种情况。

    要在这个例子中找到正确的导数,我们需要考虑求和和和积规则

    总和规则:

    产品规则:

    这意味着方程的导数计算如下

    关于x:

    关于w:

    关于b:

    渐变正好反映了:

    torch.equal(w.grad,x)#=>True
    火炬。相等(x.grad,w)#=>真
    torch.equal(b.grad,torch.tensor([[1,1],[1,1]],dtype=torch.float64))#=>True
    
    我应该在data science stack exchange上发布吗?