对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.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。
版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。