计算PyTorch中标量和向量之间的梯度
我试图将使用Theano编写的代码复制到PyTorch。在代码中,作者使用计算PyTorch中标量和向量之间的梯度,pytorch,theano,Pytorch,Theano,我试图将使用Theano编写的代码复制到PyTorch。在代码中,作者使用 import theano.tensor as T gparams = T.grad(cost, params) 而gparams的形状是(256240) 我尝试过使用backward(),但它似乎没有返回任何结果。PyTorch中是否有与grad相当的值 假设这是我的输入 import torch from torch.autograd import Variable cost = torch.tensor
import theano.tensor as T
gparams = T.grad(cost, params)
而gparams
的形状是(256240)
我尝试过使用backward()
,但它似乎没有返回任何结果。PyTorch中是否有与grad
相当的值
假设这是我的输入
import torch
from torch.autograd import Variable
cost = torch.tensor(1.6019)
params = Variable(torch.rand(1, 73, 240))
成本
需要是涉及参数
的操作的结果。你不能只知道两个张量的值就计算梯度。你也需要了解他们之间的关系。这就是为什么pytorch在执行张量运算时构建计算图的原因。例如,假设关系是
cost = torch.sum(params)
然后,我们期望成本
相对于参数
的梯度为一个向量,而不管参数
的值如何
这可以计算如下。请注意,您需要添加requires_grad
标志,以向pytorch指示您希望backward
在调用时更新渐变
# Initialize independent variable. Make sure to set requires_grad=true.
params = torch.tensor((1, 73, 240), requires_grad=True)
# Compute cost, this implicitly builds a computation graph which records
# how cost was computed with respect to params.
cost = torch.sum(params)
# Zero the gradient of params in case it already has something in it.
# This step is optional in this example but good to do in practice to
# ensure you're not adding gradients to existing gradients.
if params.grad is not None:
params.grad.zero_()
# Perform back propagation. This is where the gradient is actually
# computed. It also resets the computation graph.
cost.backward()
# The gradient of params w.r.t to cost is now stored in params.grad.
print(params.grad)
结果:
tensor([1., 1., 1.])
调用cost.backward()时,参数不在计算图中。您需要了解如何连接参数和成本。