We are about to switch to a new forum software. Until then we have removed the registration on this forum.

- All Categories 25.7K
- Announcements & Guidelines 13
- Common Questions 30
- Using Processing 22.1K
- Programming Questions 12.2K
- Questions about Code 6.4K
- How To... 4.2K
- Hello Processing 72
- GLSL / Shaders 292
- Library Questions 4K
- Hardware, Integration & Other Languages 2.7K
- Kinect 668
- Arduino 1K
- Raspberry PI 188
- Questions about Modes 2K
- Android Mode 1.3K
- JavaScript Mode 413
- Python Mode 205
- Questions about Tools 100
- Espanol 5
- Developing Processing 548
- Create & Announce Libraries 211
- Create & Announce Modes 19
- Create & Announce Tools 29
- Summer of Code 2018 93
- Rails Girls Summer of Code 2017 3
- Summer of Code 2017 49
- Summer of Code 2016 4
- Summer of Code 2015 40
- Summer of Code 2014 22
- p5.js 1.6K
- p5.js Programming Questions 947
- p5.js Library Questions 315
- p5.js Development Questions 31
- General 1.4K
- Events & Opportunities 289
- General Discussion 365

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

Edit your post. Select your code. Press Ctrl + o.

Thanks TfGuy44!