Time to play!

Now let's apply these classes and get some results. The following code has a test class, a main method with an object of the NeuralNet class called nn. We are going to define a simple neural network with two inputs, one output, and one hidden layer containing three neurons:

public class NeuralNetConsoleTest {
  public static void main(String[] args) {
    RandomNumberGenerator.seed=0;
       
    int numberOfInputs=2;
    int numberOfOutputs=1;
    int[] numberOfHiddenNeurons= { 3 };
    IActivationFunction[] hiddenAcFnc = { new Sigmoid(1.0) } ;
    Linear outputAcFnc = new Linear(1.0);
    System.out.println("Creating Neural Network...");
    NeuralNet nn = new NeuralNet(numberOfInputs,numberOfOutputs,
          numberOfHiddenNeurons,hiddenAcFnc,outputAcFnc);
    System.out.println("Neural Network created!");
    nn.print();
    …
}

Still in this code, let's feed to the neural network two sets of data, and let's see what output it is going to produce:

  double [] neuralInput = { 1.5 , 0.5 };
  double [] neuralOutput;
  System.out.println("Feeding the values ["+String.valueOf(neuralInput[0])+" ; "+
                String.valueOf(neuralInput[1])+"] to the neural network");
  nn.setInputs(neuralInput);
  nn.calc();
  neuralOutput=nn.getOutputs();
  
  neuralInput[0] = 1.0;
  neuralInput[1] = 2.1;
  ...
  nn.setInputs(neuralInput);
  nn.calc();
  neuralOutput=nn.getOutputs();

This code gives the following output:

Time to play!

It's relevant to remember that each time that the code runs, it generates new pseudo random weight values, unless you work with the same seed value. If you run the code exactly as provided here, the same values will appear in console:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset