標籤:建立 參數 計算 自己的 模組 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的訓練過程