Skip to content

Instantly share code, notes, and snippets.

@y-abe
Created September 27, 2015 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-abe/2bceb8dccd947ff1d36b to your computer and use it in GitHub Desktop.
Save y-abe/2bceb8dccd947ff1d36b to your computer and use it in GitHub Desktop.
require 'nn'
dataset = {}
function dataset:size() return 1000 end
for i = 1, dataset:size() do
local input = torch.randn(2)
print(input)
local output = torch.Tensor(1)
if input[1] * input[2] > 0 then
output[1] = 1
else
output[1] = -1
end
dataset[i] = {input, output}
end
mlp = nn.Sequential()
inputs = 2
outputs = 1
hiddens = 20
mlp:add( nn.Linear(inputs, hiddens) )
mlp:add( nn.Tanh() )
mlp:add( nn.Linear(hiddens, outputs) )
criterion = nn.MSECriterion()
trainer = nn.StochasticGradient(mlp, criterion)
trainer.learningRate = 0.01
trainer:train(dataset)
x = torch.Tensor(2)
x[1] = 0.5; x[2] = 0.5; print( mlp:forward(x) )
x[1] = 0.5; x[2] = -0.5; print( mlp:forward(x) )
x[1] = -0.5; x[2] = 0.5; print( mlp:forward(x) )
x[1] = -0.5; x[2] = -0.5; print( mlp:forward(x) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment