We have 5 steps to do in training a torch neural network.
*(1) Load and normalize data; *
*(2) Define Neural Network; *
*(3) Define Loss function; *
*(4) Train network on training data; *
*(5) Test network on test data. *
Code
1 | require 'paths' |
Load and normalize data
1 | if (not paths.filep("cifar10torchsmall.zip")) then |
1 | trainset.data = trainset.data:double() |
Define neural network
1 | net = nn.Sequential() |
Define the Loss function
1 | criterion = nn.ClassNLLCriterion() |
Train the neural network
1 | trainer = nn.StochasticGradient(net, criterion) |
1 | -- train on GPU |
Test the network, print accuracy
1 | correct = 0 |
Result
It’ll take around 30 minutes to get the result on CPU. However, it’ll cost less than 10 minutes on GPU.
1 | th filename.lua |