MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
mnist_simple_mlnn.cpp
Go to the documentation of this file.
1 
16 /*
17 * @Author: kmrocki/tkornuta
18 * @Date: 2016-02-24 09:43:05
19 */
20 
21 #include <logger/Log.hpp>
22 #include <logger/ConsoleOutput.hpp>
23 using namespace mic::logger;
24 
25 #include <iomanip>
26 
27 #include <importers/MNISTMatrixImporter.hpp>
28 #include <encoders/MatrixXfMatrixXfEncoder.hpp>
29 #include <encoders/UIntMatrixXfEncoder.hpp>
30 
32 
33 // Using multi layer neural networks
34 using namespace mic::mlnn;
35 using namespace mic::types;
36 
37 int main() {
38  // Task parameters.
39  size_t batch_size = 20;
40  size_t iterations = 60000/batch_size;
41 
42  // Set console output.
43  LOGGER->addOutput(new ConsoleOutput());
44 // LOGGER->setSeverityLevel(LTRACE);
45 
46  // Create a simple NN for classification (should give around 95.3% accuracy)
47  //MNIST - 28x28 -> 256 -> 100 -> 10
48  BackpropagationNeuralNetwork<float> nn("3layerReLUSofmax");
49  /*nn.pushLayer(new Linear<float>(28 * 28, 256));
50  nn.pushLayer(new ReLU<float>(256));
51  nn.pushLayer(new Linear<float>(256, 100));
52  nn.pushLayer(new ReLU<float>(100));
53  nn.pushLayer(new Linear<float>(100, 10));
54  nn.pushLayer(new Softmax<float>(10));
55  */
56  nn.pushLayer(new mic::mlnn::convolution::Convolution<float>(28, 28, 1, 20, 14, 7));
57  nn.pushLayer(new ReLU<float>(180));
58  nn.pushLayer(new Linear<float>(180, 10));
59  nn.pushLayer(new Softmax<float>(10));
60  nn.verify();
61 
62  //[60000, 784]
63  // Load the MNIST training...
64  mic::importers::MNISTMatrixImporter<float> training;
65  // Manually set paths. DEPRICATED! Used here only for simplification of the test.
66  training.setDataFilename("../data/mnist/train-images.idx3-ubyte");
67  training.setLabelsFilename("../data/mnist/train-labels.idx1-ubyte");
68  training.setBatchSize(batch_size);
69 
70  if (!training.importData())
71  return -1;
72 
73  // ... and test datasets.
74  mic::importers::MNISTMatrixImporter<float> test;
75  // Manually set paths. DEPRICATED! Used here only for simplification of the test.
76  test.setDataFilename("../data/mnist/t10k-images.idx3-ubyte");
77  test.setLabelsFilename("../data/mnist/t10k-labels.idx1-ubyte");
78  test.setBatchSize(batch_size);
79 
80  if (!test.importData())
81  return -1;
82 
83  // Initialize the encoders.
84  mic::encoders::MatrixXfMatrixXfEncoder mnist_encoder(28, 28);
85  mic::encoders::UIntMatrixXfEncoder label_encoder(10);
86 
87 
88  LOG(LSTATUS) << "Starting the training of neural network...";
89  float learning_rate = 0.001;
90  MatrixXfPtr encoded_batch, encoded_targets;
91 
92  // Perform the training.
93  for (size_t ii = 0; ii < iterations; ii++) {
94  LOG(LINFO) << "Batch " << std::setw(4) << ii << "/" << std::setw(4) << iterations;
95 
96  // Get random batch [784 x batch_size].
97  MNISTBatch<float> rand_batch = training.getRandomBatch();
98  encoded_batch = mnist_encoder.encodeBatch(rand_batch.data());
99  encoded_targets = label_encoder.encodeBatch(rand_batch.labels());
100 
101  // Train network with batch.
102  float loss = nn.train (encoded_batch, encoded_targets, learning_rate);
103  LOG(LINFO) << "Training: loss = " << std::setprecision(8) << loss;
104  }//: for
105  LOG(LSTATUS) << "Training finished";
106 
107  // Check performance on the test dataset.
108  LOG(LSTATUS) << "Calculating performance for test dataset...";
109  size_t correct = 0;
110  float loss = 0.0;
111  test.setNextSampleIndex(0);
112  while(!test.isLastBatch()) {
113 
114  // Get next batch [784 x batch_size].
115  MNISTBatch<float> next_batch = test.getNextBatch();
116  encoded_batch = mnist_encoder.encodeBatch(next_batch.data());
117  encoded_targets = label_encoder.encodeBatch(next_batch.labels());
118 
119  // Test network response.
120  // Skip dropout layers at test time
121  nn.forward(encoded_batch, true);
122  // Get predictions.
123  mic::types::MatrixXfPtr encoded_predictions = nn.getPredictions();
124  // Calculate the loss and correct predictions.
125  loss += nn.calculateMeanLoss(encoded_targets, encoded_predictions);
126  correct += nn.countCorrectPredictions(encoded_targets, encoded_predictions);
127 
128  }//: while
129  double test_acc = (double)correct / (double)(test.size());
130  LOG(LINFO) << "Test : loss = " << std::setprecision(3) << loss << " correct = " << std::setprecision(3) << 100.0 * test_acc << " %";
131 
132  // Check performance on the training dataset.
133  LOG(LSTATUS) << "Calculating performance for the training dataset...";
134  correct = 0;
135  loss = 0;
136  training.setNextSampleIndex(0);
137  while(!training.isLastBatch()) {
138 
139  // Get next batch [784 x batch_size].
140  MNISTBatch<float> next_batch = training.getNextBatch();
141  encoded_batch = mnist_encoder.encodeBatch(next_batch.data());
142  encoded_targets = label_encoder.encodeBatch(next_batch.labels());
143 
144  // Test network response.
145  // Skip dropout layers at test time
146  nn.forward(encoded_batch, true);
147  // Get predictions.
148  mic::types::MatrixXfPtr encoded_predictions = nn.getPredictions();
149  // Calculate the loss and correct predictions.
150  loss += nn.calculateMeanLoss(encoded_targets, encoded_predictions);
151  correct += nn.countCorrectPredictions(encoded_targets, encoded_predictions);
152  }
153  double train_acc = (double)correct / (double)(training.size());
154  LOG(LINFO) << "Train : loss = " << std::setprecision(3) << loss << " correct = " << std::setprecision(3) << 100.0 * train_acc << " %";
155 
156 }
mic::encoders::UIntMatrixXfEncoder * label_encoder
Label 2 matrix encoder (1 hot).
int main()
size_t countCorrectPredictions(mic::types::MatrixPtr< eT > targets_, mic::types::MatrixPtr< eT > predictions_)
mic::types::MatrixPtr< eT > getPredictions()
eT train(mic::types::MatrixPtr< eT > encoded_batch_, mic::types::MatrixPtr< eT > encoded_targets_, eT learning_rate_, eT decay_=0.0f)
const size_t batch_size
void forward(mic::types::MatrixPtr< eT > input_data, bool skip_dropout=false)
mic::encoders::ColMatrixEncoder< double > * mnist_encoder
MNIST matrix encoder.
eT calculateMeanLoss(mic::types::MatrixPtr< eT > encoded_targets_, mic::types::MatrixPtr< eT > encoded_predictions_)