Let the implementations begin! Neural networks in practice

In this book, we will cover the entire process of implementing a neural network by using the Java programming language. Java is an object-oriented programming language that was created in the 1990s by a small group of engineers from Sun Microsystems, later acquired by Oracle in the 2010s. Nowadays, Java is present in many devices that are part of our daily life.

In an object-oriented language, such as Java, we deal with classes and objects. A class is a blueprint of something in the real world, and an object is an instance of this blueprint, something like a car (class referring to all and any car) and my car (object referring to a specific car—mine). Java classes are usually composed of attributes and methods (or functions), that include objects-oriented programming (OOP) concepts. We are going to briefly review all of these concepts without diving deeper into them, since the goal of this book is just to design and create neural networks from a practical point of view. Four concepts are relevant and need to be considered in this process:

  • Abstraction: The transcription of a real-world problem or rule into a computer programming domain, considering only its relevant features and dismissing the details that often hinder development.
  • Encapsulation: Analogous to a product encapsulation by which some relevant features are disclosed openly (public methods), while others are kept hidden within their domain (private or protected), therefore avoiding misuse or excess of information.
  • Inheritance: In the real world, multiple classes of objects share attributes and methods in a hierarchical manner; for example, a vehicle can be a superclass for car and truck. So, in OOP, this concept allows one class to inherit all features from another one, thereby avoiding the rewriting of code.
  • Polymorphism: Almost the same as inheritance, but with the difference that methods with the same signature present different behaviors on different classes.

Using the neural network concepts presented in this chapter and the OOP concepts, we are now going to design the very first class set that implements a neural network. As can be seen, a neural network consists of layers, neurons, weights, activation functions, and biases, and there are basically three types of layers: input, hidden, and output. Each layer may have one or more neurons. Each neuron is connected either to a neural input/output or to another neuron, and these connections are known as weights.

It is important to highlight that a neural network may have many hidden layers or none, as the number of neurons in each layer may vary. However, the input and output layers have the same number of neurons as the number of neural inputs/outputs, respectively.

So, let's start implementing. Initially, we are going to define six classes, detailed as follows:

Class name: Neuron

Attributes

private ArrayList<Double> listOfWeightIn

An ArrayList variable of real numbers that represents the list of input weights

private ArrayList<Double> listOfWeightOut

An ArrayList variable of real numbers that represents the list of output weights

Methods

public double initNeuron()

Initializes listOfWeightIn and listOfWeightOut function with a pseudo random real number

Parameters: None

Returns: A pseudo random real number

public void setListOfWeightIn(ArrayList<Double> listOfWeightIn)

Sets the listOfWeightIn function with a list of real numbers list

Parameters: The list of real numbers to be stored in the class object

Returns: None

public void setListOfWeightOut(ArrayList<Double> listOfWeightOut)

Sets the listOfWeightOut function with a list of real numbers list

Parameters: The list of real numbers to be stored in the class object

Returns: None

public ArrayList<Double> getListOfWeightIn()

Returns the input weights a list of neurons

Parameters: None

Returns: The list of real numbers stored in the listOfWeightIn variable

public ArrayList<Double> getListOfWeightOut()

Returns the output weights a list of neurons

Parameters: None

Returns: The list of real numbers stored in the listOfWeightOut variable

Class implementation with Java: file Neuron.java

Class Name: Layer

Note: This class is abstract and cannot be instantiated.

Attributes

private ArrayList<Neuron> listOfNeurons

An ArrayList variable of objects of the Neuron class

private int numberOfNeuronsInLayer

Integer number to store the quantity of neurons that are part of the layer

Methods

public ArrayList<Neuron> getListOfNeurons()

Returns the list of neurons by layer

Parameters: None

Returns: An ArrayList variable of objects by the Neuron class

public void setListOfNeurons(ArrayList<Neuron> listOfNeurons)

Sets the listOfNeurons function with an ArrayList variable of objects of the Neuron class

Parameters: The list of objects of the Neuron class to be stored

Returns: None

public int getNumberOfNeuronsInLayer()

Returns the number of neurons by layer

Parameters: None

Returns: The number of neurons by layer

public void setNumberOfNeuronsInLayer(int numberOfNeuronsInLayer)

Sets the number of neurons in a layer

Parameters: The number of neurons in a layer

Returns: None

Class implementation with Java: file Layer.java

Class name: InputLayer

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

Attributes

None

Methods

public initLayer(InputLayer inputLayer)

Initializes the input layer with pseudo random real numbers

Parameters: An object of the InputLayer class

Returns: None

public void printLayer(InputLayer inputLayer)

Prints the input weights of the layer

Parameters: An object of the InputLayer class

Returns: None

Class implementation with Java: file InputLayer.java

Class name: HiddenLayer

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

Attributes

None

Methods

public ArrayList<HiddenLayer> initLayer(HiddenLayer hiddenLayer, ArrayList<HiddenLayer> listOfHiddenLayer, InputLayer inputLayer, OutputLayer outputLayer)

Initializes the hidden layer(s) with pseudo random real numbers

Parameters: An object of the HiddenLayer class, a list of objects of the HiddenLayer class, an object of the InputLayer class, an object of the OutputLayer class

Returns: None

public void printLayer(ArrayList<HiddenLayer> listOfHiddenLayer)

Prints the weights of the layer(s)

Parameters: A list of objects of the HiddenLayer class

Returns: None

Class implementation with Java: file HiddenLayer.java

Class name: OutputLayer

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

Attributes

None

Methods

public OutputLayer initLayer(OutputLayer outputLayer)

Initializes the output layer with pseudo random real numbers

Parameters: An object of the OutputLayer class

Returns: None

public void printLayer(OutputLayer outputLayer)

Prints the weights of the layer

Parameters: An object of the OutputLayer class

Returns: None

Class implementation with Java: file OutputLayer.java

Class name: NeuralNet

Note: The values of the neural net topology are fixed in this class (two neurons in the input layer, two hidden layers with three neurons each, and one neuron in the output layer). Reminder: It's the first version.

Attributes

private InputLayer inputLayer;

An object of the InputLayer class

private HiddenLayer hiddenLayer;

An object of the HiddenLayer class

private ArrayList<HiddenLayer> listOfHiddenLayer;

An ArrayList variable of objects of the HiddenLayer class. It is possible to have more than one hidden layer

private OutputLayer outputLayer;

An object of the OutputLayer class

private int numberOfHiddenLayers;

Integer number to store the quantity of layers that are part of the hidden layer

Methods

public void initNet()

Initializes the neural net as a whole. Layers are built, and each list of the weights of neurons is built randomly

Parameters: None

Returns: None

public void printNet()

Prints the neural net as a whole. Each input and output weight of each layer is shown

Parameters: None

Returns: None

Class implementation with Java: file NeuralNet.java

One advantage of OOP languages is the ease to document the program in Unified Modeling Language (UML). UML class diagrams present classes, attributes, methods, and relationships between classes in a very simple and straightforward manner, thus helping the programmer and/or stakeholders to understand the project as a whole. The following figure represents the very first version of the project's class diagram:

Let the implementations begin! Neural networks in practice

Now, let's apply these classes and get some results. The code shown next has a test class, a main method with an object of the NeuralNet class called n. When this method is called (by executing the class), it calls the initNet() and printNet()methods from the object n, generating the following result shown in the figure right after the code. It represents a neural network with two neurons in the input layer, three in the hidden layer, and one in the output layer:

public class NeuralNetTest {
  public static void main(String[] args) {
    NeuralNet n = new NeuralNet();
    n.initNet();
    n.printNet();

  }
}

It's relevant to remember that each time that the code runs, it generates new pseudo random weight values. So, when you run the code, the other values will appear in Console:

Let the implementations begin! Neural networks in practice
..................Content has been hidden....................

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