Coding of the neural network learning

Now, it is time to develop a neural network using OOP concepts and explain the related theory. The project presented in the previous chapter was adapted to implement the perceptron and adaline rules, as well as the Delta rule.

The NeuralNet class presented in the previous chapter has been updated to include the training dataset (input and target output), learning parameters, and activation function settings. The InputLayer function was also updated to include one method. We added to the project the Adaline, Perceptron, and Training classes. Details on the implementation of each class can be found in the codes. However, now, let's make the connection between the neural learning and the Java implementation of the Training class.

Learning parameter implementation

The Training class should be used for training neural networks. In this chapter, we are going to use this class to train Perceptron and Adaline classes. Also, the activation functions that are foreseen to be used in the neural networks in this chapter should be considered. So, now, let's define two enumeration sets that will handle these settings:

  public enum TrainingTypesENUM {
    PERCEPTRON, ADALINE;
  }

  public enum ActivationFncENUM {
    STEP, LINEAR, SIGLOG, HYPERTAN;
  }

In addition to these parameters, we need to define the condition for stopping, the error, the MSE error, and the number of epochs, as shown in the following code:

  private int epochs;
  private double error;
  private double mse;

The learning rate has already been defined in the NeuralNet class and will be used here.

Finally, we need a method to update the weights of a given neuron. So, let's take a look at the CalcNewWeight method:

  private double calcNewWeight(TrainingTypesENUM trainType,
      double inputWeightOld, NeuralNet n, double error,
      double trainSample, double netValue) {
    switch (trainType) {
    case PERCEPTRON:
      return inputWeightOld + n.getLearningRate() * error * trainSample;
    case ADALINE:
      return inputWeightOld + n.getLearningRate() * error * trainSample
          * derivativeActivationFnc(n.getActivationFnc(), netValue);
    default:
      throw new IllegalArgumentException(trainType
          + " does not exist in TrainingTypesENUM");
    }
  }

We see in this method a switch clause that selects the update procedure according to the training type (Adaline or Perceptron). We can also see the inputWeightOld (the old weights), n (neural network under training), error (difference between target and neural output), trainsample (input to the weight), and netValue (weighted sum before processing by activation function) parameters. The learning rate is retrieved by calling the getLearningRate() function of the NeuralNet class.

One interesting detail is the derivative of the activation function that is called for the Adaline training type, which is the Delta rule. All the activation functions are implemented as methods inside the Training class, and their respective derivatives are implemented as well. The derivativeActivationFnc method helps to call the derivative corresponding to the activation function passed in the argument.

Learning procedure

Two special methods are implemented in the Training class: one for training the neural network and the other for training the neurons of some layer. Although this won't be necessary in this chapter, it is always good to have a code prepared for future examples or updates. Let's take a quick look at the implementation of the method train:

public NeuralNet train(NeuralNet n) {
    
    ArrayList<Double> inputWeightIn = new ArrayList<Double>();

    int rows = n.getTrainSet().length;
    int cols = n.getTrainSet()[0].length;

    while (this.getEpochs() < n.getMaxEpochs()) {

      double estimatedOutput = 0.0;
      double realOutput = 0.0;

      for (int i = 0; i < rows; i++) {

        double netValue = 0.0;

        for (int j = 0; j < cols; j++) {
          inputWeightIn = n.getInputLayer().getListOfNeurons().get(j)
              .getListOfWeightIn();
          double inputWeight = inputWeightIn.get(0);
          netValue = netValue + inputWeight * n.getTrainSet()[i][j];
        }

        estimatedOutput = this.activationFnc(n.getActivationFnc(),
            netValue);
        realOutput = n.getRealOutputSet()[i];

        this.setError(realOutput - estimatedOutput);

        if (Math.abs(this.getError()) > n.getTargetError()) {
          // fix weights
          InputLayer inputLayer = new InputLayer();
          inputLayer.setListOfNeurons(this.teachNeuronsOfLayer(cols,
              i, n, netValue));
          n.setInputLayer(inputLayer);
        }

      }

      this.setMse(Math.pow(realOutput - estimatedOutput, 2.0));
      n.getListOfMSE().add(this.getMse());

      this.setEpochs(this.getEpochs() + 1);

    }

    n.setTrainingError(this.getError());

    return n;
  }

This method receives a neural network in the parameter and produces another neural network with trained weights. Further, we see a while clause that loops while the number of epochs does not reach the maximum set out in the Training class. Inside this loop, there is a for clause that iterates over all the training samples that are presented to the network and so begins the process of calculating the neural output for the input in the current iteration.

When it gets the real output of the network, it compares it to the estimated output and calculates the error. This error is checked, and if it is higher than the minimum error, then it starts the update procedure by calling the teachNeuronsOfLayer method in the following line:

inputLayer.setListOfNeurons(this.teachNeuronsOfLayer(cols,
              i, n, netValue));

The implementation of this method is found in the codes attached with this chapter.

Then, this process is repeated iteratively until all the neural samples are passed to the neural network, and then, until the maximum number of epochs is reached.

Class definitions

The following table shows all the fields and methods for all the classes covered in this chapter:

Class name: Training

Note: This class is abstract and cannot be instantiated.

Attributes

private int epochs

Integer number to store the training cycle, known as epoch

private double error

Real number to store the error between estimated output and real output

private double mse

Real number to store the mean square error (MSE)

Enums

Note: enum helps to control different types

public enum TrainingTypesENUM {
  PERCEPTRON, ADALINE;
}

Enumeration to store types of training supported by project (Perceptron and Adaline)

public enum ActivationFncENUM {
  STEP, LINEAR, SIGLOG, HYPERTAN;
}

Enumeration to store types of activation functions supported by project (step, linear, sigmoid logistics, and hyperbolic tangent)

Methods

public NeuralNet train(NeuralNet n)

Trains the neural network

Parameters: NeuralNet object (neural net untrained)

Returns: NeuralNet object (neural net trained)

public ArrayList<Neuron> teachNeuronsOfLayer(int numberOfInputNeurons, int line, NeuralNet n, double netValue)

Teaches neurons of the layer, calculating and changing its weights

Parameters: Number of input neurons, samples line, NeuralNet object, neural net output

Returns: ArrayList of objects by the Neuron class

private double calcNewWeight(TrainingTypesENUM trainType, double inputWeightOld, NeuralNet n, double error, double trainSample, double netValue)

Calculates the new weight of a neuron

Parameters: Train type enum value, old input weight value, NeuralNet object, error value, training sample value, output net value

Returns: Real number represents a new weight value

public double activationFnc ( ActivationFncENUM fnc, double value)

Decides which activation function to use and calls the method of computing it

Parameters: Activation function enum value, real number value

Returns: Calculated value of the activation function

public double derivativeActivationFnc (
ActivationFncENUM fnc, double value)

Decides which activation function to use and calls the method of computing the derivative value

Parameters: Activation function enum value, real number value

Returns: Calculated value of the derivative of the activation function

private double fncStep (double v)

Computes step function

Parameters: Real number value

Returns: Real number value

private double fncLinear (double v)

Computes linear function

Parameters: Real number value

Returns: Real number value

private double fncSigLog (double v)

Computes sigmoid logistics function

Parameters: Real number value

Returns: Real number value

private double fncHyperTan (double v)

Computes hyperbolic tangent function

Parameters: Real number value

Returns: Real number value

private double derivativeFncLinear (double v)

Computes the derivative of the linear function

Parameters: Real number value

Returns: Real number value

private double derivativeFncSigLog (double v)

Computes the derivative of the sigmoid logistics function

Parameters: Real number value

Returns: Real number value

private double derivativeFncHyperTan (double v)

Computes the derivative of the hyperbolic tangent function

Parameters: Real number value

Returns: Real number value

public void printTrainedNetResult (NeuralNet trainedNet)

Prints trained neural net and shows its results

Parameters: NeuralNet object

Returns: None

public int getEpochs()

Returns the number of epochs of the training

public void setEpochs (int epochs)

Sets the number of epochs of the training

public double getError()

Returns the training error (comparison between estimated and real values)

public void setError (double error)

Sets the training error

public double getMse()

Returns the MSE

public void setMse (double mse)

Sets the MSE

Class implementation with Java: file Training.java

Class name: Perceptron

Note: This class inherits attributes and methods from the Training class

Attributes

None

Method

public NeuralNet train(NeuralNet n)

Trains the neural network using the perceptron algorithm

Parameters: NeuralNet object (neural net untrained)

Returns: NeuralNet object (neural net trained via Perceptron)

Class implementation with Java: file Perceptron.java

Class name: Adaline

Note: This class inherits attributes and methods from the Training class.

Attributes

None

Method

public NeuralNet train(NeuralNet n)

Trains the neural network using the adaline algorithm

Parameters: NeuralNet object (neural net untrained)

Returns: NeuralNet object (neural net trained via adaline)

Class implementation with Java: file Adaline.java

Class name: InputLayer

Note: This class already existed in the previous version and has been updated as follows:

Attributes

None

Method

public void setNumberOfNeuronsInLayer( int numberOfNeuronsInLayer)

Sets the number of neurons in the input layer. It increased by one because of the bias

Class implementation with Java: file InputLayer.java

Class name: NeuralNet

Note: This class already existed in the previous version and has been updated as follows:

Attributes

private double[][] trainSet  

Matrix to store the training set of input data

private double[] realOutputSet

Vector to store the training set of output data

private int maxEpochs

Integer number to store the maximum number of epochs that neural net will train

private double learningRate

Real number to store the learning rate

private double targetError

Real number to store the target error

private double trainingError

Real number to store the training error

private TrainingTypesENUM trainType

Enum value of the training type that will be used to train the neural net

private ActivationFncENUM activationFnc

Enum value of the activation function that will be used in training

private ArrayList<Double> listOfMSE = new ArrayList<Double>()

ArrayList of real numbers to store the MSE error of each epoch

Methods

public NeuralNet trainNet (NeuralNet n)

Trains the neural network

Parameters: NeuralNet object (neural net untrained)

Returns: NeuralNet object (neural net trained)

public void printTrainedNetResult ( NeuralNet n)

Prints the trained neural net and shows its results

Parameters: NeuralNet object

Returns: None

public double[][] getTrainSet()

Returns the matrix of the training set of input data

public void setTrainSet(double[][] trainSet)

Sets the matrix of the training set of input data

public double[] getRealOutputSet()

Returns the vector training set of output data

public void setRealOutputSet(double[] realOutputSet)

Sets the vector training set of output data

public int getMaxEpochs()

Returns the maximum number of epochs that the neural net will train

public void setMaxEpochs(int maxEpochs)

Sets the maximum number of epochs that the neural net will train

public double getTargetError()

Returns the target error

public void setTargetError(double targetError)

Sets the target error

public double getLearningRate()

Returns the learning rate used in training

public void setLearningRate(double learningRate)

Sets the learning rate used in training

public double getTrainingError()

Returns the training error

public void setTrainingError(double trainingError)

Sets the training error

public ActivationFncENUM getActivationFnc()

Returns the enum value of the activation function that will be used in training

public void setActivationFnc( ActivationFncENUM activationFnc)

Sets the enum value of the activation function that will be used in training

public TrainingTypesENUM getTrainType()

Returns the enum value of the training type that will be used to train the neural net

public void setTrainType( TrainingTypesENUM trainType)

Sets the enum value of the training type that will be used to train the neural net

public ArrayList<Double> getListOfMSE()

Returns the list of real numbers that stores the MSE error of each epoch

public void setListOfMSE( ArrayList<Double> listOfMSE)

Sets the list of real numbers that stores the MSE error of each epoch

Class implementation with Java: file NeuralNet.java

The updated class diagram is shown in the following figure. Attributes and methods already explained in the previous chapter were omitted. Further, configuration methods of new attributes (setters and getters) were also omitted.

Class definitions
..................Content has been hidden....................

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