Shiraz University - CSE Dept - Project # 2

Training Neural Networks Using Genetic Algorithms

M.M.Haji
mehdi.haji@gmail.com


Problem Definition:

Genetic Algorithms can be applied to the problem of neural network design in several ways, in this project we will explore the use of GAs in training a network of known structure ( a 3-2-1 feed forward network ). GAs can also be applied to discover the size, structure and learning parameters of a network to be trained by a separate learning algorithm.



A 3-2-1 Feed Forward Neural Network

The training set is the 3-bit XOR function, in the following form:

0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0

1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 1

Where in each line the first three numbers are inputs and the last number is the output resulting from XORing these three inputs.

Our aim is to find the set of input-hidden weights, hidden layer biases, hidden-output weights and output layer biases using a GA for the above training set.

 

Implementation:

The implementation includes three java files:

As you see, the implementation is so flexible and general and is not limited to this specific XOR problem. By only modifying lines 1,2 and 3, the program can be used to train another function to the network

Download class files here: NNUtils.class, FFNN.class and GAPopulation.class.

Compile and Run:

To compile and run the program, java 2 complier must be installed on your system. If so, execute the following commands:


  javac NNUtils.java
  javac FFNN.java
  javac GAPopulation.java
  java GAPopulation

 

Results:

With 2 neurons in the hidden layer, it is hard for GA to find a set pf appropriate weights, but after some runs, the following weights was found:

input to hidden weight matrix:

-0.983598    4.8899565
1.0686378    -6.306935
-1.001853    5.6389494

hidden layer bias:

0.40927365    -1.8566227

hidden to output weight matrix:

15.7031975
11.211046

output layer bias:

0.32423866

 

But with 3 neurons in the hidden layer, almost in each run this GA converges (i.e. finds the set of optimal weights).