[3] Example - wouterkistemaker/Neural-Network GitHub Wiki

Example

In this section, an example can be found of a neural network that has been trained to a specific input.

Creating the neural network

NeuralNetwork network = new NeuralNetwork.Builder(
                new InputLayer(3, false, new SigmoidFunction(), new MeanSquaredFunction()),
                new DenseLayer(2, false, new SigmoidFunction(), new MeanSquaredFunction()))
                .withDenseLayer(new DenseLayer(10, false, new SigmoidFunction(), new MeanSquaredFunction()))
                .withDenseLayer(new DenseLayer(5, false, new SigmoidFunction(), new MeanSquaredFunction()))
                .withLearningRate(0.01)
                .withTargetOutput(0.3, 0.8)
                .withRandomStartValues()
                .withInput(0.1, 0.2, 0.9).build();

Training the neural network

final int resultEpochs = network.train(1000 * 30); // 30 seconds

The int resultEpochs is the amount of training-cycles the neural network ran in the 30 seconds it trained itself

Saving the neural network

final File file = new File("neural_network.txt");
network.saveNetwork(file);

Loading the neural network

final File file = new File("neural_network.txt");
final NeuralNetwork network = network.loadNetwork(file);

Running the neural network

final double[] output = network.compute(0.1, 0.2, 0.9); 

Keep in mind that, in the Training Section, the network was trained ONLY to the input (0.1, 0.2, 0.9). This means that putting in a wildly random other number between 0 and 1 will not result in any accurate results.

This example has no practical use at all, it just demonstrates how this library can be used. To make a more useful example, one would have to feed in a lot of different data and 'tell' the network (by specifying the desired output) whether the output is correct or not. This way, the network will know what to do with a lot of different data and running the network on a new test example will then result in a good accurate result most probably.