(原)torch的訓練過程

來源:互聯網
上載者:User

標籤:建立   參數   計算   自己的   模組   model   forward   pre   參考   

轉載請註明出處:

http://www.cnblogs.com/darkknightzh/p/6221622.html

參考網址:

http://ju.outofmemory.cn/entry/284587

https://github.com/torch/nn/blob/master/doc/criterion.md

 

假設已經有了model=setupmodel(自己建立的模型),同時也有自己的訓練資料input,實際輸出outReal,以及損失函數criterion(參見第二個網址),則使用torch訓練過程如下:

1 -- given model, criterion, input, outReal2 model:training()3 model:zeroGradParameters()4 outPredict = model:forward(input)5 err= criterion:forward(outPredict, outReal)6 grad_criterion = criterion:backward(outPredict, outReal)7 model:backward(input, grad_criterion)8 model:updateParameters(learningRate)

上面第1行假定已知的參數

第2行設定為訓練模式

第3行將model中每個模組儲存的梯度清零(防止之前的幹擾此次迭代)

第4行將輸入input通過model,得到預測的輸出outPredict

第5行通過損失Function Compute在當前參數下模型的預測輸出outPredict和實際輸出outReal的誤差err

第6行通過預測輸出outPredict和實際輸出outReal計算損失函數的梯度grad_criterion

第7行反向計算model中每個模組的梯度

第8行更新model每個模組的參數

 

每次迭代時,均需要執行第3行至第8行。

(原)torch的訓練過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.