Saving your Neural Network Training Progress

If you are familiar with Neural Networks you know that training one is very time consuming, it can take days, weeks or even months, the time will depend on the purpose of your application and the hardware you have available. But regardless of the time, you don’t want to lose the progress after training your neural network, otherwise you would have to train it again every time you run your application.

In this tutorial I’m going to show a very easy way to store and load your network data. I’m going to use Encog Framework from Heaton Reasearch, it’s a great machine learning framework, if you don’t know it yet, it’s definitely worth checking out.

So let’s get started, if you are reading this tutorial you probably already have your network ready, but for those of you who don’t, here is an example of how to create one:

BasicNetwork network = new BasicNetwork();
network.addLayer(new BasicLayer(null, true, 2));
network.addLayer(new BasicLayer(new ActivationSigmoid(), true, 2));
network.addLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
network.getStructure().finalizeStructure();
network.reset();

This is a really simple one, I’m not going to explain the details about this code because that’s not what this tutorial is about, but I promise I’ll write another one explaining more about it.

Now let’s suppose you spent hours training it and you don’t want to lose everything, you can solve this with just one single line of code, here is how you can save the progress:

EncogDirectoryPersistence.saveObject(new File("C:/networkData.eg"), network);

It couldn’t be easier than that! Basically, the only thing you have to do is to pass the path, the file name and the network to the EncogDirectoryPersistence.saveObject method, it will generate and save for you an .eg file containing the network data. If you check the folder after executing this line the file will probably be there.

Loading that file is equally easy, instead of creating your network like did in the example above, you just have to do call the method EncogDirectoryPersistence.loadObject passing the file you want to load, like this:

BasicNetwork network = (BasicNetwork) EncogDirectoryPersistence.loadObject(new File("C:/networkData.eg"));

That’s it! I’ve just recreated my network with the data I saved before, now I could train it a little bit more and then save it again overriding the same file.

Hope you liked the tutorial, leave a comment if you have any doubts, I’ll be very happy to help!

Leave a Reply

Your email address will not be published. Required fields are marked *

Obs: Use the tag <pre lang="LANGUAGE"> to include code blocks to your comment.
Example: <pre lang="javascript"> console.log('Test'); </pre>