首先根據源文檔中的ReLU(x)=max(0,x),得出結論。大於0的數值不變,小於0的數據變成0。
補充:這里需要注意的是 ReLU並沒有限制數據的大小。
這是對應的文檔鏈接:https://pytorch.org/docs/1.2.0/nn.html#torch.nn.ReLU Ps:需要自取。
參數:inplace為True,將會改變輸入的數據 ,否則不會改變原輸入,只會產生新的輸出。
好處:省去了反復申請與釋放內存的時間,直接代替原來的值。
測試代碼:
import torch from torch import nn m = nn.ReLU() # 隨機生成5個數 有正有負。 input = torch.randn(5) # 打印 隨機生成的數 print(input) output = m(input) # 經過nn.ReLU()作用之后的數 print(output) # 結果 # tensor([-0.7706, -0.1823, 0.2687, 0.2796, -1.7201]) # tensor([0.0000, 0.0000, 0.2687, 0.2796, 0.0000])
任務完成,下班。