Created
September 20, 2017 16:02
-
-
Save twmht/62181428e907dae6101cc97356e01195 to your computer and use it in GitHub Desktop.
torch simple neural network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # reference https://ckmarkoh.github.io/blog/2017/01/01/torch-nn-tutorial-4-backward-propagation/ | |
| # we want to learn y=x+1 | |
| require 'nn' | |
| y = torch.Tensor(5) | |
| mlp = nn.Sequential() | |
| mlp:add(nn.Add(5)) | |
| function gradUpdate(mlp, x, y, criterion, learningRate) | |
| local pred = mlp:forward(x) | |
| local err = criterion:forward(pred, y) | |
| local gradCriterion = criterion:backward(pred, y) | |
| mlp:zeroGradParameters() | |
| mlp:backward(x, gradCriterion) | |
| mlp:updateParameters(learningRate) | |
| return err | |
| end | |
| for i = 1, 20 do | |
| print (i) | |
| x = torch.rand(5) | |
| y:copy(x); | |
| for i = 1, 5 do y[i] = y[i] + i; end | |
| err = gradUpdate(mlp, x, y, nn.MSECriterion(), 0.01) | |
| end | |
| print(mlp:get(1).bias) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment