Created
September 27, 2015 16:07
-
-
Save y-abe/2bceb8dccd947ff1d36b 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' | |
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