Tutorials References Menu

Artificial Intelligence

Training a Perceptron

  • Create a Perceptron Object
  • Create the Training Points
  • Compute Desired answers
  • Train the perceptron against desired answers

Task

Imagine a straight line in a space with scattered x y points.

The line is a graph with the formula "y = f(x) = x*1.2 + 50".

Train a perceptron to classify the points over and under the line.


Create a Perceptron Object

Create a Perceptron object. Name it anything (like Perceptron).

Remember from the Perceptron chapter that the bias should always be 1.

Then create random weights between -1 and 1 for each input.

Example

function Perceptron(no) {

this.learnc = 0.01;
this.bias = 1;
this.weights = [];

for (let i = 0; i <= no; i++) {
  this.weights[i] = Math.random() * 2 - 1;
}
.
.

Add an Activate Function

Remember the perceptron algorithm:

  • Multiply each input with the perceptron's weights
  • Sum the results
  • Compute the outcome

Example

this.activate = function(inputs) {
  let sum = 0;
  for (let i = 0; i < inputs.length; i++) {
    sum += inputs[i] * this.weights[i];
  }
  if (sum > 0) {return 1;} else {return -1;}
}

Create a Training Function

The training function guesses the outcome based on the activate function.

Every time the guess is wrong, the perceptron should adjust the weights.

After many guesses and adjustments, the weights will be correct.

Example

this.train = function(inputs, desired) {
  inputs.push(this.bias);
  let guess = this.activate(inputs);
  let error = desired - guess;
  if (error != 0) {
    for (let i = 0; i < inputs.length; i++) {
      this.weights[i] += this.learnc * error * inputs[i];
    }
  }
}

Create Random X Y Points

Create as many xy points as wanted.

Let the x values be random, between 0 and maximum.

Let the y values be random, between 0 and maximum.

Example

xPoints = [];
yPoints = [];
for (let i = 0; i < numPoints; i++) {
  xPoints[i] = Math.random() * xMax;
  yPoints[i] = Math.random() * yMax;
}

Compute Desired Answers

Compute the desired answers based on the known formula.

Desired answer is -1 if y is under the line, and +1 if y is over the line.

Store the desired answers in an array (desired).

Example

let desired = [];
for (let i = 0; i < numPoints; i++) {
  let x = xPoints[i];
  let y = yPoints[i];
  let answer = -1;
  if (y > x) {answer = 1;}
  this.desired[i] = answer;
}

Try it Yourself »


Create Your Own Library

Library Code

function Perceptron(no) {

// Set Variables and Weights
this.learnc = 0.01;
this.bias = 1;
this.weights = [];
for (let i = 0; i <= no; i++) {
  this.weights[i] = Math.random() * 2 - 1;
}

// Activate Function
this.activate = function(inputs) {
  let sum = 0;
  for (let i = 0; i < inputs.length; i++) {
    sum += inputs[i] * this.weights[i];
  }
  if (sum > 0) {return 1;} else {return -1;}
}

// Train Function
this.train = function(inputs, desired) {
  inputs.push(this.bias);
  let guess = this.activate(inputs);
  let error = desired - guess;
  if (error != 0) {
    for (let i = 0; i < inputs.length; i++) {
      this.weights[i] += this.learnc * error * inputs[i];
    }
  }
}

// End Perceptron Object
}

Now you can include the library in HTML:

<script src="myperceptron.js"></script>

Try it Yourself »