pytorch 學習遇到的 問題

2021-10-23 14:10:35 字數 1336 閱讀 2486

relu 和 leaky_relu 中 inplace 設定

nn.relu(inplace=true) #default inplace=false

nn.leakyrelu(inplace=true)#default inplace=false

"""將計算得到的值直接覆蓋之前的值 有時能夠節省運算記憶體,不用多儲存其他變數,只要不報錯 就可以使用

如果出現梯度** 需要將 inplace 設定為 false

目前碰到 nn.leakyrelu(inplace=true) 出現梯度增大問題

設定為true後,輸出資料會覆蓋輸入資料,導致無法求取relu的梯度,

一般在模型測試的時候,設定為true是沒有問題的,

但是在訓練過程中,這樣做就會導致梯度傳遞出現問題,所以會導致loss飆公升,訓練失敗.

"""

2 nn.sequential() 內部模型傳入多個引數

class layer(nn.module):

def __init__(self):

super(layer, self).__init__()

def forward(self, x, y):

return x, y

model = nn.sequential(

layer(),

layer(),

layer()

)model(2, 3)

# 報錯

#typeerror: forward() takes 2 positional arguments but 3 were given

需要自定義 nn.sequential()

class mysequential(nn.sequential):

def forward(self, *input):

for module in self._modules.values():

input = module(*input)

return input

class layer(nn.module):

def __init__(self):

super(layer, self).__init__()

def forward(self, x, y):

return x, y

model = mysequential(

layer(),

layer(),

layer()

)model(2, 3)

#process finished with exit code 0

安裝pytorch遇到的問題

git clone recursive pip install r requirements.txt 這步操作超級慢 python setup.py install 安裝成功後,測試 python import torch 出現錯誤 參考 cd usr local lib python3.5 dis...

pytorch 遇到的小問題總結

一 transpose 是permute的精簡版本。transpose dim1,dim2 只能將兩個維度進行互換 y y.permute 2,3,1,4 按照2,3,1,4維進行重排列二 對兩個variable進行concat操作,按道理實現方式是c torch.cat a,b dim 0 但提示...

Pytorch深度學習遇到的坑

記錄學習過程中遇到的一些問題以及解決方案。在繼承了nn.module的情況下,並且函式內數 算均為torch內建函式 運算過程不能夠破壞pytorch計算圖,否則梯度無法正確反向傳播 依舊無法呼叫 初始化有問題可以嘗試在類建構函式末尾加入return 呼叫方式錯誤在呼叫構造類物件時,不能直接進行呼叫...