Example – Perceptron

This is the coding for the Perceptron that we saw earlier. The code running it is P5 Java and it is fairly straightforward. If you want to play with the code you will see below the code structure. I will add a few hints below the code – All you need to do is open Notepad or Gedit hit the copy button and paste the code into Notepad. Then save the code as say perceptron.html then open perceptron.html with your browser and it will run as below.

The Code Structure

Things you can change:

( First save the code as is so that any changes you make can be rectified )

Looking through the code above you will see the following lines:

// The function to describe a line
function f(x) {
// let y = 0 * x + 0.1;
let y = 0.3 * x + 0.2;
// let y = -0.3 * x + 0.2;

These are the ‘white line’ functions that delineate the ‘Stars’ and ‘Planets’ in our example – the first line y=0x+0.1 is the straight line across the screen. The second line y=0.3x+0.2 is the current line drawn upwards from left to right. The third y=-0.3*x+0.2 is a line drown downwards from left to right or – you can add any function as long as it is in the form of y=mx+c

The next is the number of data points printed on the screen:

// A list of points we will use to “train” the perceptron

let datapoints=400;

You can change the number of data points on the screen – the original was 2000

The final one is the learning rate which you can find here:

// The perceptron has 3 inputs — x, y, and bias
// Second value is “Learning Constant”
ptron = new Perceptron(3, 0.001); // Learning Constant is set low

Here you can change the value 0.001 to anything you wish – within reason.

That’s about it – any other changes you make may result in the code not working well – In which case you will need to download it again -good luck !