脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pytorch Variable与Tensor合并后 requires_grad()默认与修改方式

pytorch Variable与Tensor合并后 requires_grad()默认与修改方式

2021-10-28 09:13西电小猪猪 Python

这篇文章主要介绍了pytorch Variable与Tensor合并后 requires_grad()默认与修改方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

pytorch更新完后合并了Variable与Tensor

torch.Tensor()能像Variable一样进行反向传播的更新,返回值为Tensor

Variable自动创建tensor,且返回值为Tensor,(所以以后不需要再用Variable)

Tensor创建后,默认requires_grad=Flase

可以通过xxx.requires_grad_()将默认的Flase修改为True

下面附代码及官方文档代码:

?
1
2
3
4
5
6
7
8
9
10
import torch
from torch.autograd import Variable #使用Variabl必须调用库
lis=torch.range(1,6).reshape((-1,3))#创建1~6 形状
#行不指定(-1意为由计算机自己计算)列为3的floattensor矩阵
 
print(lis)
print(lis.requires_grad) #查看默认的requires_grad是否是Flase
 
lis.requires_grad_() #使用.requires_grad_()修改默认requires_grad为true
print(lis.requires_grad)

结果如下:

tensor([[1., 2., 3.],
[4., 5., 6.]])
False
True

创建一个Variable,Variable必须接收Tensor数据 不能直接写为 a=Variable(range(6)).reshape((-1,3))

否则报错 Variable data has to be a tensor, but got range

正确如下:

?
1
2
3
4
5
6
7
8
9
import torch
from torch.autograd import Variable
tensor=torch.FloatTensor(range(8)).reshape((-1,4))
my_ten=Variable(tensor)
print(my_ten)
print(my_ten.requires_grad)
 
my_ten.requires_grad_()
print(my_ten.requires_grad)

结果:

tensor([[0., 1., 2., 3.],
[4., 5., 6., 7.]])
False
True

由上面可以看出,Tensor完全可以取代Variable。

下面给出官方文档:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 默认创建requires_grad = False的Tensor 
x = torch . ones ( 1 ) # create a tensor with requires_grad=False (default)
x . requires_grad
# out: False
 
# 创建另一个Tensor,同样requires_grad = False
y = torch . ones ( 1 ) # another tensor with requires_grad=False
# both inputs have requires_grad=False. so does the output
z = x + y
# 因为两个Tensor x,y,requires_grad=False.都无法实现自动微分,
# 所以操作(operation)z=x+y后的z也是无法自动微分,requires_grad=False
z . requires_grad
# out: False
 
# then autograd won't track this computation. let's verify!
# 因而无法autograd,程序报错
z . backward ( )
# out:程序报错:RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
 
# now create a tensor with requires_grad=True
w = torch . ones ( 1 , requires_grad = True )
w . requires_grad
# out: True
 
# add to the previous result that has require_grad=False
# 因为total的操作中输入Tensor w的requires_grad=True,因而操作可以进行反向传播和自动求导。
total = w + z
# the total sum now requires grad!
total . requires_grad
# out: True
# autograd can compute the gradients as well
total . backward ( )
w . grad
#out: tensor([ 1.])
 
# and no computation is wasted to compute gradients for x, y and z, which don't require grad
# 由于z,x,y的requires_grad=False,所以并没有计算三者的梯度
z . grad == x . grad == y . grad == None
# True
existing_tensor . requires_grad_ ( )
existing_tensor . requires_grad
# out:True

或者直接用Tensor创建时给定requires_grad=True

?
1
2
3
my_tensor = torch.zeros(3,4,requires_grad = True)
my_tensor.requires_grad
# out: True
?
1
2
3
4
5
lis=torch.range(1,6,requires_grad=True).reshape((-1,3))
print(lis)
print(lis.requires_grad)
lis.requires_grad_()
print(lis.requires_grad)

结果

tensor([[1., 2., 3.],
[4., 5., 6.]], requires_grad=True)
True
True

补充:volatile 和 requires_grad在pytorch中的意思

Backward过程中排除子图

pytorch的BP过程是由一个函数决定的,loss.backward(), 可以看到backward()函数里并没有传要求谁的梯度。那么我们可以大胆猜测,在BP的过程中,pytorch是将所有影响loss的Variable都求了一次梯度。

但是有时候,我们并不想求所有Variable的梯度。那就要考虑如何在Backward过程中排除子图(ie.排除没必要的梯度计算)。

如何BP过程中排除子图? Variable的两个参数(requires_grad和volatile)

requires_grad=True 要求梯度

requires_grad=False 不要求梯度

volatile=True相当于requires_grad=False。反之则反之。。。。。。。ok

注意:如果a是requires_grad=True,b是requires_grad=False。则c=a+b是requires_grad=True。同样的道理应用于volatile

为什么要排除子图

也许有人会问,梯度全部计算,不更新的话不就得了。

这样就涉及了效率的问题了,计算很多没用的梯度是浪费了很多资源的(时间,计算机内存)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/weixin_43635550/article/details/100192797

延伸 · 阅读

精彩推荐