We show an example here on a classical XOR problem.
Dataset
We first need to create a dataset, following the conventions described in
StochasticGradient
.
require "lab" dataset={}; function dataset:size() return 100 end -- 100 examples for i=1,dataset:size() do local input = lab.randn(2); -- normally distributed example in 2d local output = torch.Tensor(1); if input[1]*input[2]>0 then -- calculate label for XOR function output[1] = -1; else output[1] = 1 end dataset[i] = {input, output} end
Neural Network
We create a simple neural network with one hidden layer.
require "nn" mlp = nn.Sequential(); -- make a multi-layer perceptron inputs = 2; outputs = 1; HUs = 20; -- parameters mlp:add(nn.Linear(inputs, HUs)) mlp:add(nn.Tanh()) mlp:add(nn.Linear(HUs, outputs))
Training
We choose the Mean Squared Error criterion and train the beast.
criterion = nn.MSECriterion() trainer = nn.StochasticGradient(mlp, criterion) trainer.learningRate = 0.01 trainer:train(dataset)
Test the network
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))
You should see something like:
> x = torch.Tensor(2) > x[1] = 0.5; x[2] = 0.5; print(mlp:forward(x)) -0.3490 [torch.Tensor of dimension 1] > x[1] = 0.5; x[2] = -0.5; print(mlp:forward(x)) 1.0561 [torch.Tensor of dimension 1] > x[1] = -0.5; x[2] = 0.5; print(mlp:forward(x)) 0.8640 [torch.Tensor of dimension 1] > x[1] = -0.5; x[2] = -0.5; print(mlp:forward(x)) -0.2941 [torch.Tensor of dimension 1]