PyTorch中ReLU的inplace

0 - inplace

  在pytorch中,nn.ReLU(inplace=True)和nn.LeakyReLU(inplace=True)中存在inplace字段。该参数的inplace=True的意思是进行原地操作,例如:

  • x=x+5是对x的原地操作
  • y=x+5,x=y不是对x的原地操作

  所以,如果指定inplace=True,则对于上层网络传递下来的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.

  如果你使用了in-place operation而没有报错的话,那么你可以确定你的梯度计算是正确的。

1 - 参考资料

https://www.jianshu.com/p/8385aa74e2de

https://blog.csdn.net/york1996/article/details/81835873

原文地址:https://www.cnblogs.com/CZiFan/p/10790765.html