torch.autograd.grad与backward

1. 进行一次torch.autograd.grad或者loss.backward()后前向传播都会清空,因此想反复传播必须要加上retain_graph=True。

2.torch.autograd.grad是返回一个列表,对应你所列参数的梯度。而backward()则是对parameter中的grad项进行赋值。

from torch.autograd import Variable
import torch
import torch.nn as nn


class g(nn.Module):
    def __init__(self):
        super(g, self).__init__()
        self.k1 = nn.Conv2d(in_channels=2, out_channels=2, kernel_size=1, padding=0, bias=False)
        self.k = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=1, padding=0, bias=False)

    def forward(self, z):
        # a, b = torch.topk(z, 2, dim=-1, largest=True, sorted=True)
        # return a
        return self.k(self.k1(z))


c = 2
h = 5
w = 5
z = torch.arange(0, c * h * w).float().view(1, c, h, w)
z = Variable(z)
gg = g()
r = gg(z)

r = r.sum()
loss = (r - 1) * (r - 1)
#
# gg.zero_grad()

grad = torch.autograd.grad(loss, gg.parameters(),retain_graph=True)
print(grad)
loss.backward()
for k,v in gg.named_parameters():
    print(k)
    print(v.grad.data)
    # print(v)

print("******************")

for k,v in gg.named_parameters():
    print(v.grad.data)
    v.data.sub_(v.grad.data * 0.01)

print("******************")

for k,v in gg.named_parameters():
    print(v)

你可能感兴趣的:(Pytorch,pytorch,python)