目录
pytroch的一些注意点
pytorch是一个很方便的深度框架,上手简单,不过也有一些需要注意的点:
- Tensor和Variable的区别
- backward的使用
Tensor和Variable的区别
-Tensor
只是一个类似与Numpy
的矩阵块,我们可以用Tensor
作为Numpy
的代替。 比Numpy
相比如果使用的是gpu版的pytorch,则我们可以选择把Tensor
存在显卡上,使用.cuda
的办法。
- Variable
不仅封装了Tensor
作为对应的激活值,还保存了产生这个Variable
的 计算图,确保之后的自动求导可行。
- 具体可以参考这篇pytorch如何构建计算图
- 用Tensor
构成的Variable
并不共享内存。
例如:
import torch
from torch.autograd import Variable
data = torch.randn(3, 4)
Data = Variable(data, requires_grad=True)
print(data)
print(Data)
"""
-0.4764 -0.3905 0.7649 0.0500
2.2558 -0.6688 0.3069 -1.1182
0.0612 0.7757 0.9996 -0.2380
[torch.DoubleTensor of size 3x4]
Variable containing:
-0.4764 -0.3905 0.7649 0.0500
2.2558 -0.6688 0.3069 -1.1182
0.0612 0.7757 0.9996 -0.2380
[torch.DoubleTensor of size 3x4]
"""
Data = Data + Variable(torch.ones(3, 4))
print(data)
print(Data)
"""
-0.4764 -0.3905 0.7649 0.0500
2.2558 -0.6688 0.3069 -1.1182
0.0612 0.7757 0.9996 -0.2380
[torch.DoubleTensor of size 3x4]
Variable containing:
0.5236 0.6095 1.7649 1.0500
3.2558 0.3312 1.3069 -0.1182
1.0612 1.7757 1.9996 0.7620
[torch.DoubleTensor of size 3x4]
"""
backward的使用
-backward
是Variable
用来计算梯度的方法。值得注意的是,如果Variable
本身是一个标量时,可以直接使用Variable.backward()
来获得所需要的导数。例如:
import torch
from torch.autograd import Variable
data = torch.randn(3, 4)
Data = Variable(data, requires_grad=True)
print(Data)
"""
Variable containing:
0.0291 -0.9512 2.1084 0.7745
-0.5678 1.6256 -0.1632 -0.0343
0.2082 1.8471 1.4175 -0.3341
[torch.FloatTensor of size 3x4]
"""
print(Data.grad)
"""
None
"""
F = torch.mean(Data + Variable(torch.ones(3, 4)))
F.backward()
print(Data.grad)
"""
Variable containing:
1.00000e-02 *
8.3333 8.3333 8.3333 8.3333
8.3333 8.3333 8.3333 8.3333
8.3333 8.3333 8.3333 8.3333
[torch.FloatTensor of size 3x4]
"""
- 如果
Variable
本身是非标量时,可以直接使用Variable.backward(a)
来获得所需要的导数(a
的大小和Variable
的大小一致)。例如:
import torch
from torch.autograd import Variable
data = torch.randn(3, 4)
x = Variable(data, requires_grad=True)
y = Variable(torch.randn(2, 3))
print(x)
print(y)
"""
Variable containing:
0.2585 0.5968 -0.1272 -1.3166
-0.0830 -0.8735 0.1632 -1.0764
-0.5808 0.5057 0.1693 0.7596
[torch.FloatTensor of size 3x4]
Variable containing:
0.6695 -1.6198 -0.5489
2.8908 0.1137 -0.7324
[torch.FloatTensor of size 2x3]
"""
print(x.grad)
"""
None
"""
loss = torch.mm(y, x) # torch.mm表示x与y做矩阵乘法,loss的shape为 2 * 4
loss.backward(torch.ones(2, 4))
print(x.grad)
"""
Variable containing:
3.5603 3.5603 3.5603 3.5603
-1.5061 -1.5061 -1.5061 -1.5061
-1.2813 -1.2813 -1.2813 -1.2813
[torch.FloatTensor of size 3x4]
"""
- 上述例子可以理解为得到的
loss
与一个和loss
一样大小的全1的矩阵,做对应位置相乘最后相加得到一个标量之后在求导。这就化成第一种情况,所以也就可以理解为loss
在每个分量上的梯度。更多例子可见pytorch自动微分的几个例子 - 需要注意的一点,pytorch的
backward
在使用过一次之后会把相关信息删除,所以每次构建的图只允许一次backward
否则会报错RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
如果要多次使用需要在backward
设置retain_variables=True
- 最后需要注意一点,使用
Variable.backward()
求Variable
的梯度的时候,Variable.grad
是累加。例子可见关于Gradient,所以每次完成后记得把梯度变回0,Variable.grad.data.zero_()
。