Skip to content

[2] Repository tutorial

Wouter Kistemaker edited this page Dec 2, 2020 · 1 revision

How to construct a neural network

An instance of NeuralNetwork is obtained by using the NeuralNetwork.Builder class, which obviously is a Builder pattern class. A neural network must always have at least

  • One input-layer (type= InputLayer)
  • One output-layer (type= DenseLayer)

The input-layer is required as a buffer for the input-values and without an output-layer, one can never access the output of the network.

Knowing this, moving on to constructing your very first instance of NeuralNetwork as follows:

        final InputLayer inputLayer = new InputLayer(2); // size is 2, so the network needs two input-values to work with
        final DenseLayer outputLayer = new DenseLayer(1); // size is 1, so the network will compute one output value
        
        final NeuralNetwork network = new NeuralNetwork.Builder(inputLayer, outputLayer)
               .withInput(0.3, 0.5) // input values
               .withTargetOutput(0.1) // wanted output value
               .build();

Congratulations, you now created your first NeuralNetwork-object. You can mess around with the settings using the builder-pattern, such as:

  • Change the initial weights to a fixed number or to random weights
  • Specify the learning rate (the stepsize of the gradient descent process, default = 0.1)
  • Add hidden-layers (type=DenseLayer)

How to train the network

To train the network, one can either use the train() or train(long duration) function. This first function executes 1 training-cycle, so to have an accurate training-process, one will have to use a loop to execute this task a lot of times (Everything above 10.000 times will probably produce very accurate results). The second functions takes an amount of milliseconds to work with, so 5000 means the network will train itself for 5 seconds.

Keep in mind that one second already executes the train-cycle between 20.000 and 40.000 times (partly based on your CPU).

How to save/load the network

Assuming that, after a training-session, the network has run a lot of training-cycles and thus updated the internal weights a lot of times as well. To improve the effectiveness and efficiency of future usage of the network, one might want to store the network. This allows us to load the network with the same updated internal weights. When this is accomplished, a new training-session might be redundant because the network can already produce accurate results. Thus, saving and loading a network is very useful and done as follows:

Saving

To save a network, one will have to define an instance of java.io.File, as the function NeuralNetwork.save(File file) takes it as parameter. Simply call the function with the file (network.save(file);) and you are good to go.

Loading

To load a network, one will logically have to retrieve an instance of java.io.File of the file where the network was previously stored. Once the object of this File is created, one can achieve an object of NeuralNetwork using the static function NeuralNetwork.loadNetwork(File file);.

This time, making use of the Builder-pattern is useless since we already have a completely configured network stored in a File. Since we don't know what this configuration is, we cannot fill in the Builder's parameters.

One can now create an object of NeuralNetwork as follows:

final NeuralNetwork network = NeuralNetwork.loadNetwork(file);