remake of the chapter 9's SineWave Predictor - but with an introduced error
Hi,
I'm attempting to do a remake of chapter 9's example from the book by using the encog 2.1
classes directly in my example.
However, I suspect I may have an issue with my TemporalNeuralDataSet...data..which
is causing the error message at the bottom of this post.
I'm not sure where to look next..I suspect the data..But, when I look at the input
and the ideal values..the values look correct..
not sure why the error below is appearing..
many thanks,
-Terry
------------
package com.linuxclicks.neural.examples.encog.tests.sine;
import org.encog.neural.activation.ActivationTANH;
import org.encog.neural.data.temporal.TemporalDataDescription;
import org.encog.neural.data.temporal.TemporalNeuralDataSet;
import org.encog.neural.data.temporal.TemporalPoint;
import org.encog.neural.data.temporal.TemporalDataDescription.Type;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.training.anneal.NeuralSimulatedAnnealing;
import org.encog.neural.networks.training.propagation.back.Backpropagation;
public class Predictor
{
public final static int ACTUAL_SIZE = 500;
public final static int TRAINING_SIZE = 250;
public final static int INPUT_SIZE = 5;
public final static int OUTPUT_SIZE = 1;
public final static int NEURONS_HIDDEN_1 = 7;
public final static boolean USE_BACKPROP = true;
private BasicNetwork network;
//
// actual data used for the training and validation
//
private Data data;
private TemporalNeuralDataSet temporalDataSet;
private double input[][];
private double ideal[][];
public static void main(String[] args)
{
Predictor predictor = new Predictor();
predictor.generateData();
predictor.createNetwork();
predictor.generateTrainingSets();
// predictor.trainNetworkBackprop();
predictor.trainNetworkAnneal();
}
private void generateData()
{
//
// assign the 'Data' class to a member variable..
//
this.data = new Data(Predictor.ACTUAL_SIZE, Predictor.INPUT_SIZE, Predictor.OUTPUT_SIZE);
}
private void generateTrainingSets()
{
this.temporalDataSet = new TemporalNeuralDataSet(Predictor.INPUT_SIZE, Predictor.OUTPUT_SIZE);
//
// create an array which hold have the values 250 for taining..and 5
// values used to predict the next output..
//
// for each index..hold the inputs in one array
// and for the output in another array
//
this.input = new double[Predictor.TRAINING_SIZE][Predictor.INPUT_SIZE];
this.ideal = new double[Predictor.TRAINING_SIZE][Predictor.OUTPUT_SIZE];
this.temporalDataSet.addDescription(new TemporalDataDescription(Type.RAW, true, false));
this.temporalDataSet.addDescription(new TemporalDataDescription(Type.RAW, false, true));
for (int i = 0; i < Predictor.TRAINING_SIZE; i++)
{
TemporalPoint tp = this.temporalDataSet.createPoint(i);
tp.setData(0, this.data.actual[i]);
tp.setData(1, this.data.actual[i]);
}
this.temporalDataSet.generate();
}
public void createNetwork()
{
final ActivationTANH threshold = new ActivationTANH();
this.network = new BasicNetwork();
this.network.addLayer(new BasicLayer(threshold, true, Predictor.INPUT_SIZE));
this.network.addLayer(new BasicLayer(threshold, true, Predictor.NEURONS_HIDDEN_1));
this.network.addLayer(new BasicLayer(threshold, true, OUTPUT_SIZE));
this.network.reset();
}
private void trainNetworkBackprop()
{
final Backpropagation train = new Backpropagation(this.network, this.temporalDataSet, 0.001, 0.1);
int epoch = 1;
do
{
train.iteration();
System.out.println("Iteration #" + epoch + " Error:" + train.getError());
epoch++;
}
while ((epoch < 5000) && (train.getError() > 0.03));
}
private void trainNetworkAnneal()
{
// train the neural network
final NeuralSimulatedAnnealing train = new NeuralSimulatedAnnealing(this.network, this.temporalDataSet, 10, 2, 100);
int epoch = 1;
do
{
train.iteration();
System.out.println("Iteration #" + epoch + " Error:" + train.getError());
epoch++;
}
while ((train.getError() > 0.01));
}
}
package com.linuxclicks.neural.examples.encog.tests.sine;
/**
* the data used for the training and validation.. being able to grab certain
* segments of the data..
*
* @author Terry
*
*/
public class Data
{
//
// the total number of elements in the data..
//
public final double actual[];
//
// the number of elements used to predict the next value in the
// line
//
private final int inputSize;
//
// predict only 1 bar ahead
//
private final int outputSize;
//
// retrieve the calculation of the curve at any particular point..
//
public static double calculateSine(final double deg)
{
final double rad = deg * (Math.PI / 180);
final double result = Math.sin(rad);
return ((int) (result * 100000.0)) / 100000.0;
}
//
// create all the actual data which will be used in all the training
// and validation
//
public Data(final int size, final int inputSize, final int outputSize)
{
//
// make an array for which to hold every element in the training and
// validation
// steps
//
this.actual = new double[size];
//
// the number of elements to look at simultaneously to predict the next
// value
//
this.inputSize = inputSize;
//
// the predicted value..of 1 moment in the future..
//
this.outputSize = outputSize;
//
// the starting position of the sine wave line..
//
int angle = 0;
//
// go through the number of elements in the all acutal data
// and place it into an array..
//
for (int i = 0; i < this.actual.length; i++)
{
//
// save the value in the actual data array
//
this.actual[i] = Data.calculateSine(angle);
//
// update the angle for the next iteration
//
angle += 10;
}
}
//
// input data is the data used to predict the output data...we
// are going to use 5 bars of input..but this may be variable..
//
public void getInputData(final int offset, final double target[])
{
//
// loop through the number of input elements used to predict an
// output element..
//
for (int i = 0; i < this.inputSize; i++)
{
//
// from an offset position from the actual data.. grab the
// actual input data..
//
// and place the input field data results into the target array..
//
target[i] = this.actual[offset + i];
}
}
//
// grab the actual real value of output which too meausre our predictions
// against..
//
public void getOutputData(final int offset, final double target[])
{
//
// loop throught the number of output bars.. which should just be
// 1 output bar in our prediction model..
//
for (int i = 0; i < this.outputSize; i++)
{
//
// grab data from the offset..the a number of bars which is are
// the bars used for prediction..and then grab the offset to the
// actual predicted values..
//
target[i] = this.actual[offset + this.inputSize + i];
}
}
}
43 [main] INFO org.encog.neural.networks.training.anneal.NeuralSimulatedAnnealing - Performing Simulated Annealing iteration.
Exception in thread "main" java.lang.NullPointerException
at org.encog.neural.networks.logic.FeedforwardLogic.compute(Unknown Source)
at org.encog.neural.networks.BasicNetwork.compute(Unknown Source)
at org.encog.neural.networks.BasicNetwork.calculateError(Unknown Source)
at org.encog.neural.networks.training.anneal.NeuralSimulatedAnnealing$SimulatedAnnealingHelper.determineError(Unknown Source)
at org.encog.solve.anneal.SimulatedAnnealing.iteration(Unknown Source)
at org.encog.neural.networks.training.anneal.NeuralSimulatedAnnealing.iteration(Unknown Source)
at com.linuxclicks.neural.examples.encog.tests.sine.Predictor.trainNetworkAnneal(Predictor.java:116)
at com.linuxclicks.neural.examples.encog.tests.sine.Predictor.main(Predictor.java:41)
success
I was making my network incorrectly..
package com.linuxclicks.neural.examples.encog.tests.sine;
import org.encog.neural.activation.ActivationTANH;
import org.encog.neural.data.temporal.TemporalDataDescription;
import org.encog.neural.data.temporal.TemporalNeuralDataSet;
import org.encog.neural.data.temporal.TemporalPoint;
import org.encog.neural.data.temporal.TemporalDataDescription.Type;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.training.anneal.NeuralSimulatedAnnealing;
import org.encog.neural.networks.training.anneal.NeuralTrainingSetSimulatedAnnealing;
import org.encog.neural.networks.training.propagation.back.Backpropagation;
public class Predictor
{
public final static int ACTUAL_SIZE = 500;
public final static int TRAINING_SIZE = 250;
public final static int INPUT_SIZE = 5;
public final static int OUTPUT_SIZE = 1;
public final static int NEURONS_HIDDEN_1 = 7;
public final static boolean USE_BACKPROP = true;
private BasicNetwork network;
//
// actual data used for the training and validation
//
private Data data;
private TemporalNeuralDataSet temporalDataSet;
private double input[][];
private double ideal[][];
public static void main(String[] args)
{
Predictor predictor = new Predictor();
predictor.generateData();
predictor.createNetwork();
predictor.generateTrainingSets();
// predictor.trainNetworkBackprop();
predictor.trainNetworkAnneal();
}
private void generateData()
{
//
// assign the 'Data' class to a member variable..
//
this.data = new Data(Predictor.ACTUAL_SIZE, Predictor.INPUT_SIZE, Predictor.OUTPUT_SIZE);
}
public void createNetwork()
{
this.network = new BasicNetwork();
this.network.addLayer(new BasicLayer(new ActivationTANH(), true, Predictor.INPUT_SIZE));
this.network.addLayer(new BasicLayer(new ActivationTANH(), true, Predictor.NEURONS_HIDDEN_1));
this.network.addLayer(new BasicLayer(new ActivationTANH(), true, OUTPUT_SIZE));
this.network.getStructure().finalizeStructure();
// NetworkCODEC.arrayToNetwork(Predictor.RANDOM_NET, this.network);
}
private void generateTrainingSets()
{
this.temporalDataSet = new TemporalNeuralDataSet(Predictor.INPUT_SIZE, Predictor.OUTPUT_SIZE);
//
// create an array which hold have the values 250 for taining..and 5
// values used to predict the next output..
//
// for each index..hold the inputs in one array
// and for the output in another array
//
this.input = new double[Predictor.TRAINING_SIZE][Predictor.INPUT_SIZE];
this.ideal = new double[Predictor.TRAINING_SIZE][Predictor.OUTPUT_SIZE];
this.temporalDataSet.addDescription(new TemporalDataDescription(Type.RAW, true, false));
this.temporalDataSet.addDescription(new TemporalDataDescription(Type.RAW, false, true));
for (int i = 0; i < Predictor.TRAINING_SIZE; i++)
{
TemporalPoint tp = this.temporalDataSet.createPoint(i);
tp.setData(0, this.data.actual[i]);
tp.setData(1, this.data.actual[i]);
}
this.temporalDataSet.generate();
}
private void trainNetworkBackprop()
{
final Backpropagation train = new Backpropagation(this.network, this.temporalDataSet, 0.001, 0.1);
int epoch = 1;
do
{
train.iteration();
System.out.println("Iteration #" + epoch + " Error:" + train.getError());
epoch++;
}
while ((epoch < 5000) && (train.getError() > 0.03));
}
private void trainNetworkAnneal()
{
NeuralSimulatedAnnealing train = new NeuralTrainingSetSimulatedAnnealing(this.network, this.temporalDataSet,
10, 2, 100);
int epoch = 1;
do
{
train.iteration();
System.out.println("Iteration #" + epoch + " Error:" + train.getError());
epoch++;
}
while ((train.getError() > 0.01));
}
}



