例如
torch.nn.ReLU(inplace=True)
表示進行原地操作,對上一層傳遞下來的tensor直接進行修改,如x=x+3;
表示新建一個變量存儲操作結果,如y=x+3,x=y;
可以節省運算內存,不用多存儲變量。
補充: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.
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 淺談PyTorch中in-place operation的含義
- pytorch中的自定義數據處理詳解
- PyTorch中的拷貝與就地操作詳解