Pytorch踩坑集錦

2021-10-24 15:34:17 字數 1104 閱讀 3437

1. gpu空間充足,但訓練和測試同時進行時,報空間不足,即runtimeerror: cuda out of memory.

答:很多博文建議batch改小,但是可能很多人的錯誤在於沒有固定網路,導致測試集進入網路時儲存了大量引數值,因此:

model.eval()

with torch.no_grad():

for k, test_data in enumerate(test_loader):

2. soft-argmax梯度消失

def softargmax2d(input, beta=10):

# print(input.type)

*_, h, w = input.shape

input = beta * input.reshape(*_, h * w)

input = nn.functional.softmax(input, dim=-1)

indices_c, indices_r = np.meshgrid(

np.linspace(0, 1, w),

np.linspace(0, 1, h),

indexing='xy'

)indices_r = torch.from_numpy(np.reshape(indices_r, (-1, h * w))).float().cuda()

indices_c = torch.from_numpy(np.reshape(indices_c, (-1, h * w))).float().cuda()

result_r = torch.sum((h - 1) * input * indices_r, dim=-1)

result_c = torch.sum((w - 1) * input * indices_c, dim=-1)

result = torch.stack([result_r, result_c], dim=-1)

return result

增大beta能更快得到積分數值解,即概率最大的影象座標點。但是,在求導過程中,由於soft-argmax的分母存在exp(beta*x),會導致梯度消失。所以,beta不能任意大。

開發快應用踩坑集錦

快應用開發過程踩坑集錦彙總 開發快應用學習資料及踩坑問題彙總 持續更新,調整 by qzx 參考 快應用環境搭建及安裝 環境搭建 pc安裝toolkit工具 踩坑整理 1.修改日誌輸出等級 2.console.dir 無法使用 3.安裝專案依賴必須用 npm install cnpm和yarn安裝都...

VUE 100 vue踩坑集錦

vue是允許用大寫字母來註冊元件的,但當你使用時得在駝峰命名的大寫字母間加上 並都改為小寫 中劃線 後來在官網看到這句話camelcase vs.kebab case html 特性是不區分大小寫的。所以,當使用的不是字串模版,camelcased 駝峰式 命名的 prop 需要轉換為相對應的 ke...

Pytorch多GPU訓練踩坑記錄2

使用nn.dataparallel進行多gpu訓練時,對模型進行傳參,有時會出現報錯 runtimeerror chunk expects at least a 1 dimensional tensor nn.dataparallel的作用是將模型和資料分配到各個gpu上,讓其在各自的gpu上訓練,...