对PyTorch中inplace字段的全面理解
例如
torch.nn.ReLU(inplace=True)
inplace=True
表示进行原地操作,对上一层传递下来的tensor直接进行修改,如x=x+3;
inplace=False
表示新建一个变量存储操作结果,如y=x+3,x=y;
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.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
python 中的 BeautifulSoup 网页使用方法解析
这篇文章主要介绍了python 中的 BeautifulSoup 网页使用方法解析,文章基于python的相关资料展开详细内容介绍,具有一定的参考价值需要的小伙伴可以参考一下2022-04-04
python中numpy.zeros(np.zeros)的使用方法
下面小编就为大家带来一篇python中numpy.zeros(np.zeros)的使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-11-11


最新评论