I tried to design an interface for the trainer that doesn’t deviate from the original trainer interface designed by karpathy‘s convnet.js library.
We can reuse the typical structure of the library to setup our neural network in javascript:
Here, we define a neural network with 1 input signal, a fully connected ‘relu’ (rectified linear unit layer of 12 neurons, a fully connected sigmoid layer of 8 neurons, and a regression based output layer to calculate a real number output.
Our network looks like:
Next, we would like to construct a trainer to train the network, say to do a simple data fitting exercise Y = f(X), in the datafit demo example.
When we want to train the network for a generation, we need to first define a fitnessFunction()
in js that will take a network, get it to perform a task, fit some data, or whatnot, and return a ‘fitness’ score on how that network performed so we can rank it. Please note that the fitness score must be non-positive, and the more negative the score, the worse the network is doing its job. For the case of the data fitting exercise, we can just set the fitness as the negative square error of the network output vs training samples. we can simply use the train function:
After defining the fitnessFunction()
, we can simply call the line:
fitness = trainer.train(fitnessFunction);
to perform one generation of conventional neuroevolution. After the train()
function is performed:
- the network passed to the trainer will be set to the best of 100 networks attempted.
- the fitness value of the best network will be returned
- the population of 100 networks will have evolved and have been sorted
So if the train()
function is run again, it will perform evolution again for another generation.
A useful way to use this trainer is that we can actually then use it in conjunction with the existing trainers in the convnet.js library. A useful way to come up with the initial network, is to use CNE for a few generations to come up with a good initial network choice first, and then use SGD trainers to fine tune.