In this class we are going to group the neurons that are aligned in the same layer. Also, there is a need to define links between layers, since one layer forwards values to another. So the class will have the following properties:
public abstract class NeuralLayer { protected int numberOfNeuronsInLayer; private ArrayList<Neuron> neuron; protected IActivationFunction activationFnc; protected NeuralLayer previousLayer; protected NeuralLayer nextLayer; protected ArrayList<Double> input; protected ArrayList<Double> output; protected int numberOfInputs; … }
Note that this class is abstract, the layer classes that can be instantiated are InputLayer
, HiddenLayer
, and OutputLayer
. In order to create one layer, one must use one of these classes' constructors that work quite similar:
public InputLayer(int numberofinputs); public HiddenLayer(int numberofneurons,IActivationFunction iaf, int numberofinputs); public OutputLayer(int numberofneurons,IActivationFunction iaf, int numberofinputs);
Layers are initialized and calculated as well as the neurons, they also implement the methods init()
and calc()
. The signature protected guarantees that only the subclasses can call or override these methods:
protected void init(){ for(int i=0;i<numberOfNeuronsInLayer;i++){ try{ neuron.get(i).setActivationFunction(activationFnc); neuron.get(i).init(); } catch(IndexOutOfBoundsException iobe){ neuron.add(new Neuron(numberOfInputs,activationFnc)); neuron.get(i).init(); } } } protected void calc(){ for(int i=0;i<numberOfNeuronsInLayer;i++){ neuron.get(i).setInputs(this.input); neuron.get(i).calc(); try{ output.set(i,neuron.get(i).getOutput()); } catch(IndexOutOfBoundsException iobe){ output.add(neuron.get(i).getOutput()); } } }