The neuron class

This is the very foundation class for this chapter's code. According to the theory, an artificial neuron has the following attributes:

  • Inputs
  • Weights
  • Bias
  • Activation function
  • Output

It is also important to define one attribute that will be useful in future examples, that is the output before activation function. We then have the implementation of the following properties:

public class Neuron {
  protected ArrayList<Double> weight;
  private ArrayList<Double> input;
  private Double output;
  private Double outputBeforeActivation;
  private int numberOfInputs = 0;
  protected Double bias = 1.0;
  private IActivationFunction activationFunction;
  …
}

When instantiating a neuron, we need to specify how many inputs are going to feed values to it, and what should be its activation function. So let's take a look on the constructor:

public Neuron(int numberofinputs,IActivationFunction iaf){
    numberOfInputs=numberofinputs;
    weight=new ArrayList<>(numberofinputs+1);
    input=new ArrayList<>(numberofinputs);
    activationFunction=iaf;
}

Note that we define one extra weight for the bias. One important step is the initialization of the neuron, that is, how the weights receive their first values. This is defined in the init() method, by which weights receive randomly generated values by the RandomNumberGenerator static class. Note the need to prevent an attempt to set a value outside the bounds of the weight array:

public void init(){
  for(int i=0;i<=numberOfInputs;i++){
    double newWeight = RandomNumberGenerator.GenerateNext();
    try{
      this.weight.set(i, newWeight);
    }
    catch(IndexOutOfBoundsException iobe){
      this.weight.add(newWeight);
    }
  }
}

Finally, let's take a look on how the output values are calculated in the calc() method:

public void calc(){
  outputBeforeActivation=0.0;
  if(numberOfInputs>0){
    if(input!=null && weight!=null){
      for(int i=0;i<=numberOfInputs;i++){
        outputBeforeActivation+=(i==numberOfInputs?bias:input.get(i))*weight.get(i);
      }
    }
  }
  output=activationFunction.calc(outputBeforeActivation);
}

Note that first, the products of all inputs and weights are summed (the bias multiplies the last weight – i==numberOfInputs), and this value is saved in the outputBeforeActivation property. The activation function calculates the neuron's output with this value.

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

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