MNIST inference code


We already learned how to write training code in chainer, the last task is to use this trained model to inference (predict) the test input MNIST image.

Inference code structure usually becomes as follows,

  • Prepare input data
  • Instantiate the trained model
  • Load the trained model
  • Feed input data into loaded model to get inference result

You have already learned the necessary stuff, and it is easy. See for the source code.

Prepare input data

For MNIST, it is easy in one line


Instantiate the trained model and load the model

Here, note that model can be loaded after instantiating the model. This model must have the same structure (hidden unit size, layer depth etc) when you saved the model in training stage.


Feed input data into loaded model to get inference result

Below code is to get inference result y from test input data x.


Visualize the result

You might want to see the inference result together with the input image to understand more precisely. This code draws a plot for test input image and its inference result.

Even only 50 hidden units are used, the accuracy to inference the MNIST digit number is quite high.

MNIST inference

That’s all for MNIST dataset tutorial. Now you have learned the basics of how to use deep learning framework. how to write training code, how to write inference code with Chainer. It is now ready to go further to specialized category. Convolutional Neural Network is used in wide area especially Image processing, Reccurent Neural Network is Language processing etc.


Sponsored Links

Leave a Reply

Your email address will not be published.