pytorch学习笔记——requires_grad和volatile

本片博文主要是对http://pytorch.org/docs/notes/autograd.html的部分翻译以及自己的理解,如有错误,欢迎指正!

Backward过程中排除子图

pytorchBP过程是由一个函数决定的,loss.backward(), 可以看到backward()函数里并没有传要求谁的梯度。那么我们可以大胆猜测,在BP的过程中,pytorch是将所有影响lossVariable都求了一次梯度。但是有时候,我们并不想求所有Variable的梯度。那就要考虑如何在Backward过程中排除子图(ie.排除没必要的梯度计算)。
如何BP过程中排除子图? Variable的两个参数(requires_gradvolatile

requires_grad:

import torch
from torch.autograd import Variable
x = Variable(torch.randn(5, 5))
y = Variable(torch.randn(5, 5))
z = Variable(torch.randn(5, 5), requires_grad=True)
a = x + y  # x, y的 requires_grad的标记都为false, 所以输出的变量requires_grad也为false
a.requires_grad
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
False
  • 1
  • 2
b = a + z #a ,z 中,有一个 requires_grad 的标记为True,那么输出的变量的 requires_grad为True
b.requires_grad
  • 1
  • 2
True
  • 1
  • 2

变量的requires_grad标记的运算就相当于or
如果你想部分冻结你的网络(ie.不做梯度计算),那么通过设置requires_grad标签是非常容易实现的。
下面给出了利用requires_grad使用pretrained网络的一个例子,只fine tune了最后一层。

model = torchvision.models.resnet18(pretrained=True)
for param in model.parameters():
    param.requires_grad = False
# Replace the last fully-connected layer
# Parameters of newly constructed modules have requires_grad=True by default
model.fc = nn.Linear(512, 100)

# Optimize only the classifier
optimizer = optim.SGD(model.fc.parameters(), lr=1e-2, momentum=0.9)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

volatile

j = Variable(torch.randn(5,5), volatile=True)
k = Variable(torch.randn(5,5))
m = Variable(torch.randn(5,5))
n = k+m # k,m变量的volatile标记都为False,输出的Variable的volatile标记也为false
n.volatile
  • 1
  • 2
  • 3
  • 4
  • 5
False
  • 1
  • 2
o = j+k #k,m变量的volatile标记有一个True,输出的Variable的volatile为True
o.volatile
  • 1
  • 2
True
  • 1
  • 2

变量的volatile标记的运算也相当于or
注意:volatile=True相当于requires_grad=False。但是在纯推断模式的时候,只要是输入volatile=True,那么输出Variable的volatile必为True。这就比使用requires_grad=False方便多了。

NOTE在使用volatile=True的时候,变量是不存储 creator属性的,这样也减少了内存的使用。

为什么要排除子图

也许有人会问,梯度全部计算,不更新的话不就得了。
这样就涉及了效率的问题了,计算很多没用的梯度是浪费了很多资源的(时间,计算机内存)

参考资料

http://pytorch.org/docs/notes/autograd.html

转载请注明出处。 https://blog.csdn.net/u012436149/article/details/66971822

猜你喜欢

转载自blog.csdn.net/Xs_55555/article/details/79742276