如何在pytorch中正确使用grad_fn上的next_函数[0][0]?

如何在pytorch中正确使用grad_fn上的next_函数[0][0]?,pytorch,Pytorch,我在官方pytorch教程中获得了这种nn结构: output = net(input) target = torch.randn(10) # a dummy target, for example target = target.view(1, -1) # make it the same shape as output criterion = nn.MSELoss() loss = criterion(output, target) print(loss) 输入 ->conv2d->

我在官方pytorch教程中获得了这种nn结构:

output = net(input)
target = torch.randn(10)  # a dummy target, for example
target = target.view(1, -1)  # make it the same shape as output
criterion = nn.MSELoss()

loss = criterion(output, target)
print(loss)
输入 ->conv2d->relu->MAXPOL2D->conv2d->relu->MAXPOL2D ->视图->线性->重新设置->线性->重新设置->线性 ->MSELoss ->损失

然后是一个如何使用内置的.grad_fn from变量向后跟踪梯度的示例

#例如:
打印(丢失。梯度fn)#丢失
打印(loss.grad_fn.next_函数[0][0])线性
打印(loss.grad_fn.next_函数[0][0]。next_函数[0][0])#ReLU
因此,由于给定的示例,我认为通过粘贴next_函数[0][0]9次可以到达Conv2d的grad对象,但我从索引中获得了错误元组。那么,如何正确地索引这些backprop对象呢

在运行教程中的以下内容后,在中:

output = net(input)
target = torch.randn(10)  # a dummy target, for example
target = target.view(1, -1)  # make it the same shape as output
criterion = nn.MSELoss()

loss = criterion(output, target)
print(loss)
以下代码段将打印完整的图形:

def print_graph(g, level=0):
    if g == None: return
    print('*'*level*4, g)
    for subg in g.next_functions:
        print_graph(subg[0], level+1)

print_graph(loss.grad_fn, 0)

试试跑步

打印(loss.grad\u fn.next\u函数[0][0]。next\u函数)

您将看到,这给出了一个包含三个元素的数组。它实际上是你想要选择的[1][0]元素,否则你会得到累积的梯度,你不能再进一步了。当你深入研究时,你会发现你可以一路通过网络。例如,尝试运行:

打印(loss.grad\u fn.next\u函数[0][0]。next\u函数[1][0]。next\u函数[0][0]。next\u函数[0][0]。next\u函数[1][0]。next\u函数[0][0]。next\u函数[0][0]。next\u函数

首先运行.next_函数,不在中建立索引,然后查看需要选择哪个元素才能进入nn的下一层