Introduction to Neural Networks for Java, Session 6
| Course Name | Introduction to Neural Networks for Java |
| Instructor | jeffheaton |
| Session Title | Discuss Program 1 |
| Session Number | 6 |
Session Material
I present here my solution for program 1. You can use the link below to download an Eclipse project for my solution.
First you will need to create training data in a CSV file for Program 1.
Listing: CSV Input for Program 1
0,0,0,1,0,0 0,1,0,1,0,0 1,0,0,1,0,0 1,1,1,1,0,0 0,0,0,0,1,0 0,1,1,0,1,0 1,0,1,0,1,0 1,1,1,0,1,0 0,0,0,0,0,1 0,1,1,0,0,1 1,0,1,0,0,1 1,1,0,0,0,1
Solution for program 1.
Listing: Solution for Program 1
/**
* This is an example file from the online course:
*
* Introduction to Neural Networks with Java
*
* http://www.heatonresearch.com/course/intro-neural-nets-java
*
* This course is based on the following book:
*
* Introduction to Neural Networks with Java, 2nd Edition
* Copyright 2008 by Heaton Research, Inc.
* http://www.heatonresearch.com/books/java-neural-2/
*
* ISBN13: 978-1-60439-008-7
* ISBN: 1-60439-008-5
*
* This class is released under the:
* GNU Lesser General Public License (LGPL)
* http://www.gnu.org/copyleft/lesser.html
*/
package com.heatonresearch.course.introneuralnet.program1;
import java.io.IOException;
import com.heatonresearch.book.introneuralnet.common.ReadCSV;
import com.heatonresearch.book.introneuralnet.neural.feedforward.FeedforwardLayer;
import com.heatonresearch.book.introneuralnet.neural.feedforward.FeedforwardNetwork;
import com.heatonresearch.book.introneuralnet.neural.feedforward.train.Train;
import com.heatonresearch.book.introneuralnet.neural.feedforward.train.backpropagation.Backpropagation;
/**
* This is my solution for Program 1.
*
* @author Jeff Heaton
* @version 1.0
*/
public class Program1 {
static final int COLUMNS = 6;
static final int OPP_AND = 0;
static final int OPP_OR = 1;
static final int OPP_XOR = 2;
private FeedforwardNetwork network;
private double[][] input;
private double[][] ideal;
public void createNetwork() {
this.network = new FeedforwardNetwork();
network.addLayer(new FeedforwardLayer(Program1.COLUMNS));
network.addLayer(new FeedforwardLayer(3));
network.addLayer(new FeedforwardLayer(1));
network.reset();
}
public void train() {
// train the neural network
final Train train = new Backpropagation(network, this.input,
this.ideal, 0.7, 0.8);
int epoch = 1;
do {
train.iteration();
System.out
.println("Epoch #" + epoch + " Error:" + train.getError());
epoch++;
} while ((epoch < 5000) && (train.getError() > 0.001));
}
public double evaluate(double op1, double op2, int operation) {
double[] input = new double[Program1.COLUMNS];
input[0] = op1;
input[1] = op2;
// AND
if (operation == Program1.OPP_AND)
input[2] = 1.0;
else
input[2] = 0.0;
// OR
if (operation == Program1.OPP_OR)
input[3] = 1.0;
else
input[3] = 0.0;
// XOR
if (operation == Program1.OPP_XOR)
input[4] = 1.0;
else
input[4] = 0.0;
double[] output = this.network.computeOutputs(input);
return output[0];
}
public void load(String filename) throws IOException {
int size = 0;
// count the size of the file
ReadCSV csv = new ReadCSV(filename, false);
while (csv.next()) {
size++;
}
csv.close();
// allocate enough space
this.input = new double[size][Program1.COLUMNS];
this.ideal = new double[size][1];
// now load it
int index = 0;
csv = new ReadCSV(filename, false);
while (csv.next()) {
this.input[index][0] = Double.parseDouble(csv.get(0));
this.input[index][1] = Double.parseDouble(csv.get(1));
this.input[index][2] = Double.parseDouble(csv.get(3));
this.input[index][3] = Double.parseDouble(csv.get(4));
this.input[index][4] = Double.parseDouble(csv.get(5));
this.ideal[index][0] = Double.parseDouble(csv.get(2));
index++;
}
csv.close();
}
public static void main(final String args[]) {
try {
Program1 prg = new Program1();
prg.load("c:\\data\\logic.csv");
prg.createNetwork();
prg.train();
System.out.println("1 AND 1 = "
+ prg.evaluate(1, 1, Program1.OPP_AND));
System.out.println("1 XOR 1 = "
+ prg.evaluate(1, 1, Program1.OPP_XOR));
System.out.println("1 AND 0 = "
+ prg.evaluate(1, 0, Program1.OPP_AND));
System.out.println("1 OR 1 = "
+ prg.evaluate(1, 1, Program1.OPP_OR));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Videos for this Session
| Video | Title |
|---|---|
![]() | Introduction to Neural Networks for Java(Class 6) |
