【pytorch系列】 with torch.no_grad():用法详解

参考:
https://blog.csdn.net/weixin_43178406/article/details/89517008
https://www.jianshu.com/p/1cea017f5d11
https://blog.csdn.net/weixin_44134757/article/details/105775027

在pytorch写的网络中,with torch.no_grad():非常常见。

首先,关于python中的with

with 语句适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源,比如文件使用后自动关闭/线程中锁的自动获取和释放等。

例如:

file = open("1.txt")
data = file.read()
file.close()

存在问题如下:
(1)文件读取发生异常,但没有进行任何处理;
(2)可能忘记关闭文件句柄;

改进:

try:
    f = open('xxx')
except:
    print('fail to open')
    exit(-1)
try:
    do something
except:
    do something
finally:
    f.close()

虽然这段代码运行良好,但比较冗长。
而使用with的话,能够减少冗长,还能自动处理上下文环境产生的异常。如下面代码:

with open("1.txt") as file:
    data = file.read()

总结with工作原理:
(1)紧跟with后面的语句被求值后,返回对象的“–enter–()”方法被调用,这个方法的返回值将被赋值给as后面的变量;
(2)当with后面的代码块全部被执行完之后,将调用前面返回对象的“–exit–()”方法。

关于 torch.no_grad()

首先从requires_grad讲起:

requires_grad
在pytorch中,tensor有一个requires_grad参数,如果设置为True,则反向传播时,该tensor就会自动求导。tensor的requires_grad的属性默认为False,若一个节点(叶子变量:自己创建的tensor)requires_grad被设置为True,那么所有依赖它的节点requires_grad都为True(即使其他相依赖的tensor的requires_grad = False)

当requires_grad设置为False时,反向传播时就不会自动求导了,因此大大节约了显存或者说内存。

with torch.no_grad的作用
在该模块下,所有计算得出的tensor的requires_grad都自动设置为False。

即使一个tensor(命名为x)的requires_grad = True,在with torch.no_grad计算,由x得到的新tensor(命名为w-标量)requires_grad也为False,且grad_fn也为None,即不会对w求导。例子如下所示:

x = torch.randn(10, 5, requires_grad = True)
y = torch.randn(10, 5, requires_grad = True)
z = torch.randn(10, 5, requires_grad = True)
with torch.no_grad():
    w = x + y + z
    print(w.requires_grad)
    print(w.grad_fn)
print(w.requires_grad)


False
None
False

来源:大黑山修道

物联沃分享整理
物联沃-IOTWORD物联网 » 【pytorch系列】 with torch.no_grad():用法详解

发表评论