Pytorch 中的变量和 autograd
PyTorch是 Facebook 开发的Python库,用于运行和训练机器和深度学习算法。在神经网络中,我们必须执行反向传播,这涉及优化参数以最小化其预测误差。
为此,PyTorch 提供了torch.autograd ,它通过收集所有梯度来自动区分。 Autograd通过在由函数对象组成的有向无环图中保存数据(张量)和所有已执行操作的记录来做到这一点。在这个 DAG 中,叶子是输入张量,根是输出张量。通过从根到叶跟踪这个图,我们可以使用链式法则自动计算梯度。
变量是给定正向公式的自动微分工具。它包装了一个变量。变量支持几乎所有由 Tensor 定义的 API。在定义一个变量时,我们传递参数requires_grad ,它指示变量是否可训练。默认情况下,它设置为 false。下面通过一个例子来更清楚地理解它。
示例 1:
Python3
# importing libraries
import torch
from torch.autograd import Variable
# packing the tensors with Variable
a = Variable(torch.tensor([5., 4.]), requires_grad=True)
b = Variable(torch.tensor([6., 8.]))
# polynomial function with a,b as variable
y = ((a**2)+(5*b))
z = y.mean()
print('Z value is:', z)
Python3
# importing libraries
import torch
from torch.autograd import Variable
# packing the tensors with Variable
a = Variable(torch.tensor([5., 4.]), requires_grad=True)
b = Variable(torch.tensor([6., 8.]))
# polynomial function with a,b as variable
y = ((a**2)+(5*b))
z = y.mean()
# dy/da=2*a=10,8
# dy/db=5
# computing gradient
z.backward()
# printing out
print('Gradient of a', a.grad)
print('Gradient of b', b.grad)
Python3
# importing libraries
import torch
from torch.autograd import Variable
# creating random tensors for weights and wrap them in variables
x = Variable(torch.randn(1, 10), requires_grad=False)
W = Variable(torch.randn(10, 1), requires_grad=True) # weight matrix
b = Variable(torch.randn(1), requires_grad=True) # bias vector
y = Variable(torch.tensor([[0.822]]))
# performing matrix multiplication to compute output
y_pred = torch.matmul(x, W)+b
# calculating loss
loss = (y_pred-y).pow(2)
print(loss)
# computing gradient
loss.backward()
print(W.grad)
print(b.grad)
lr = 0.001
# updating the weight matrix after backpropagation
with torch.no_grad():
W = W-(lr*W.grad.data)
print(W)
输出:
Z value is: tensor(55.5000, grad_fn=
因此,在上面的前向传递中,我们计算了在 DAG 中保持梯度函数的结果张量。之后,当backward 被调用时,它会跟随图中创建的链接向后传播以反向传播梯度并将它们累积在相应变量的grad属性中。
示例 2:
蟒蛇3
# importing libraries
import torch
from torch.autograd import Variable
# packing the tensors with Variable
a = Variable(torch.tensor([5., 4.]), requires_grad=True)
b = Variable(torch.tensor([6., 8.]))
# polynomial function with a,b as variable
y = ((a**2)+(5*b))
z = y.mean()
# dy/da=2*a=10,8
# dy/db=5
# computing gradient
z.backward()
# printing out
print('Gradient of a', a.grad)
print('Gradient of b', b.grad)
输出:
Gradient of a tensor([5., 4.])
Gradient of b None
上面你可以注意到 b 的梯度没有更新,因为在这个变量 requires_grad 没有设置为 true。这就是 Autograd 出现的地方。
Autograd是一个PyTorch包,用于对张量上的所有操作进行微分。它从一个变量开始执行反向传播。在深度学习中,这个变量通常持有成本函数的值。 Backward 执行反向传播并自动计算所有反向传播梯度。
首先,我们创建一些随机特征,例如权重和偏置向量。通过乘以 x、W 矩阵来执行线性回归。然后计算平方误差并调用backward函数进行反向传播,更新每个变量的梯度值。最后,我们优化权重矩阵并打印出来。
例子:
蟒蛇3
# importing libraries
import torch
from torch.autograd import Variable
# creating random tensors for weights and wrap them in variables
x = Variable(torch.randn(1, 10), requires_grad=False)
W = Variable(torch.randn(10, 1), requires_grad=True) # weight matrix
b = Variable(torch.randn(1), requires_grad=True) # bias vector
y = Variable(torch.tensor([[0.822]]))
# performing matrix multiplication to compute output
y_pred = torch.matmul(x, W)+b
# calculating loss
loss = (y_pred-y).pow(2)
print(loss)
# computing gradient
loss.backward()
print(W.grad)
print(b.grad)
lr = 0.001
# updating the weight matrix after backpropagation
with torch.no_grad():
W = W-(lr*W.grad.data)
print(W)
输出:
tensor([[1.3523]], grad_fn=)
tensor([[-0.4488],
[ 1.8151],
[ 3.5312],
[ 1.4467],
[ 2.8628],
[-0.9358],
[-2.7980],
[ 0.2670],
[-0.0399],
[ 0.1995]])
tensor([2.3258])
tensor([[ 1.1908],
[ 0.0301],
[-0.2003],
[ 0.6922],
[ 2.1972],
[ 0.0633],
[ 0.7101],
[-0.5169],
[ 0.7412],
[ 0.7068]])