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

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

服务器之家 - 脚本之家 - Python - 对PyTorch中inplace字段的全面理解

对PyTorch中inplace字段的全面理解

2021-11-10 10:19冷月葬婲魂 Python

这篇文章主要介绍了对PyTorch中inplace字段的全面理解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

例如

?
1
torch.nn.ReLU(inplace=True)
?
1
inplace=True

表示进行原地操作,对上一层传递下来的tensor直接进行修改,如x=x+3;

?
1
inplace=False

表示新建一个变量存储操作结果,如y=x+3,x=y;

?
1
inplace=True

可以节省运算内存,不用多存储变量。

补充:PyTorch中网络里面的inplace=True字段的意思

在例如nn.LeakyReLU(inplace=True)中的inplace字段是什么意思呢?有什么用?

inplace=True的意思是进行原地操作,例如x=x+5,对x就是一个原地操作,y=x+5,x=y,完成了与x=x+5同样的功能但是不是原地操作。

上面LeakyReLU中的inplace=True的含义是一样的,是对于Conv2d这样的上层网络传递下来的tensor直接进行修改,好处就是可以节省运算内存,不用多储存变量y。

inplace=True means that it will modify the input directly, without allocating any additional output. It can sometimes slightly decrease the memory usage, but may not always be a valid operation (because the original input is destroyed). However, if you don't see an error, it means that your use case is valid.

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u013382233/article/details/85761805

延伸 · 阅读

精彩推荐