Neural Networks - Time Series Prediction

edited April 2016 in Questions about Code

Hello All,

I have been enjoying Daniel Shiffman's Nature of Code, and I am now trying out the Neural Networks chapter. I am interested in doing some Time Series Prediction but struggling to convert the basic perceptron example to return useable data rather than -1 or 1. I understand from the next example (steering) to remove the activate function but the more I train my perceptron the more negative the numbers become?! So I must be doing something wrong and can't just return the sum.

Any advice on how to do Time Series Prediction in Processing would be greatly appreciated. I've not found many code samples for Processing or Java.

Below is the not functioning properly code, check the draw() function and the for loop for training and see how if you increase the number the returned values are more negative: ` Perceptron ptron; //2,000 training points Trainer[] training = new Trainer[2000]; int count = 10;

//The formula for a line
float f(float x) {
  return 2*x+1;
}

void setup() {
  size(640, 360);
  println("make perceptron");
  ptron = new Perceptron(3);

  //Make 2,000 training points.
  for (int i = 0; i < training.length; i++) {
    float v1 = i;//random(-width/2,width/2);
    float v2 = v1+5;//random(-height/2,height/2);
    float v3 = v2+5;
    //Is the correct answer 1 or -1?
    float answer = v3+5;
    //if (y < f(x)) answer = -1;
    training[i] = new Trainer(v1, v2, v3, answer);
  }
  println("training points made");
  noLoop();
}


void draw() {
  background(255);
  //translate(width/2,height/2);
  //println("draw");
  //  THE MORE TRAINING THE MORE NEGATIVE THE NUMBERS GET!
  for (int t=0; t<1; t++) {
    ptron.train(training[t].inputs, training[t].answer);
    //println("train...");
  }
  //For animation, we are training one point at a time.
  count = (count + 1) % training.length;
  //println("training done");
  for (int i = 0; i < count; i++) {
    stroke(0);
    float r1 = i;//random(30);
    float r2 = r1+5;//random(10);
    float r3 = r2+5;//random(10);
    float[] rs = {
      r1, r2, r3
    };
    //println("about to guess");
    float guess = ptron.feedforward(rs);//ptron.feedforward(training[i].inputs);
    //println("guessed");
    //Show the classification—no fill for -1, black for +1.
    //if (guess > 0) noFill();
    //else           fill(0);
    //ellipse(i*10, r1*10, 8, 8);
    noFill();
    ellipse(i*10, r3, 8, 8);
    fill(255, 0, 0);
    ellipse(i*10, guess, 8, 8);
    println(guess);
  }
}


class Perceptron { //The Perceptron stores its weights and learning constants.
  float[] weights;
  float c = 0.01;

  Perceptron(int n) {
    weights = new float[n]; //Weights start off random.
    for (int i = 0; i < weights.length; i++) {
      weights[i] = random(-1, 1);
    }
  }

  //Return an output based on inputs.
  float feedforward(float[] inputs) {
    float sum = 0;
    for (int i = 0; i < weights.length; i++) {
      sum += inputs[i]*weights[i];
    }
    //println(sum);
    return sum;//activate(sum);
  }

  //Output is a +1 or -1.
  float activate(float sum) {
    if (sum > 0) return 1;
    else return -1;
  }

  //Train the network against known data.
  void train(float[] inputs, float desired) {
    float guess = feedforward(inputs);
    float error = desired - guess;
    for (int i = 0; i < weights.length; i++) {
      weights[i] += c * error * inputs[i];
    }
  }
}

//
//
//

class Trainer {

  //A "Trainer" object stores the inputs and the correct answer.
  float[] inputs;
  float answer;

  Trainer(float v1, float v2, float v3, float a) {
    inputs = new float[4];
    inputs[0] = v1;
    inputs[1] = v2;
    inputs[2] = v3;
    //Note that the Trainer has the bias input built into its array.
    inputs[3] = 1;
    answer = a;
  }
}

`

Tagged:

Answers

Sign In or Register to comment.