Created
December 12, 2018 07:58
-
-
Save zldrobit/9ec147eb401a81e0d8c19de264447c9f to your computer and use it in GitHub Desktop.
This file contains 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
require 'nn'; | |
require 'nngraph'; | |
require 'optim'; | |
autograd = require 'autograd'; | |
-- W0 = torch.ones(3, 1) | |
params = {W=torch.rand(3,1)} | |
optimState = {learningRate=0.001} | |
criterion = autograd.nn.MSECriterion() | |
function neuralNet(params, x, y) | |
local out = x * params.W | |
return criterion(out, y) | |
end | |
dneuralNet = autograd(neuralNet) | |
batch_size = 5 | |
input_dim = 3 | |
function get_sample() | |
local input = torch.rand(batch_size, input_dim) | |
-- local label = torch.cmul(input, input) | |
-- local label = torch.sum(label, 2) | |
local label = torch.sum(input, 2) | |
return input, label | |
end | |
local feval = function(params) | |
-- do something here | |
dparams, loss = dneuralNet(params, input, label) | |
return dparams, loss | |
end | |
local optimfn, states = autograd.optim.sgd(feval, optimState, params) | |
steps = 10000 | |
log_per_steps = 1000 | |
for i = 1,steps do | |
input, label = get_sample() | |
local grads, loss = optimfn(input, label) | |
if i % log_per_steps == 0 then | |
print(i, loss) | |
end | |
end | |
print(params.W) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment