Training a Perceptron

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

Training Task

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

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).

Let the perceptron accept two parameters:

  • The number of inputs (no)
  • The learning rate (learningRate).
  • Set the default learning rate to 0.00001.

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

    Example

    // Perceptron Object
    function Perceptron(no, learningRate = 0.00001) {

    // Set Initial Values
    this.learnc = learningRate;
    this.bias = 1;

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

    // End Perceptron Object
    }

    The Random Weights

    The Perceptron will start with a random weight for each input.

    The Learning Rate

    For each mistake, while training the Perceptron, the weights will be adjusted with a small fraction.

    This small fraction is the "Perceptron's learning rate".

    In the Perceptron object we call it learnc.

    The Bias

    Sometimes, if both inputs are zero, the perceptron might produce an in correct output.

    To avoid this, we give the perceptron an extra input with the value of 1.

    This is called a bias.

    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 0}
    }

    The activation function will output:

  • 1 if the sum is greater than 0
  • 0 if the sum is less than 0
  • 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];
        }
      }
    }

    Try it Yourself »

    Backpropagation

    After each guess, the perceptron calculates how wrong the guess was.

    If the guess is wrong, the perceptron adjusts the bias and the weights so that the guess will be a little bit more correct the next time.

    This type of learning is called backpropagation.

    After trying (a few thousand times) your perceptron will become quite good at guessing.

    Create Your Own Library

    Library Code

    // Perceptron Object
    function Perceptron(no, learningRate = 0.00001) {

    // Set Initial Values
    this.learnc = learningRate;
    this.bias = 1;

    // Compute Random Weights
    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 0}
    }

    // 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>

    Use Your Library

    Example

    // Initiate Values
    const numPoints = 500;
    const learningRate = 0.00001;

    // Create a Plotter
    const plotter = new XYPlotter("myCanvas");
    plotter.transformXY();
    const xMax = plotter.xMax;
    const yMax = plotter.yMax;
    const xMin = plotter.xMin;
    const yMin = plotter.yMin;

    // Create Random XY Points
    const xPoints = [];
    const yPoints = [];
    for (let i = 0; i < numPoints; i++) {
      xPoints[i] = Math.random() * xMax;
      yPoints[i] = Math.random() * yMax;
    }

    // Line Function
    function f(x) {
      return x * 1.2 + 50;
    }

    //Plot the Line
    plotter.plotLine(xMin, f(xMin), xMax, f(xMax), "black");

    // Compute Desired Answers
    const desired = [];
    for (let i = 0; i < numPoints; i++) {
      desired[i] = 0;
      if (yPoints[i] > f(xPoints[i])) {desired[i] = 1}
    }

    // Create a Perceptron
    const ptron = new Perceptron(2, learningRate);

    // Train the Perceptron
    for (let j = 0; j <= 10000; j++) {
      for (let i = 0; i < numPoints; i++) {
        ptron.train([xPoints[i], yPoints[i]], desired[i]);
      }
    }

    // Display the Result
    for (let i = 0; i < numPoints; i++) {
      const x = xPoints[i];
      const y = yPoints[i];
      let guess = ptron.activate([x, y, ptron.bias]);
      let color = "black";
      if (guess == 0) color = "blue";
      plotter.plotPoint(x, y, color);
    }

    Try it Yourself »

    // Line Function function f(x) { return x * 1.2 + 50; } // Create a Plotter let plotter = new XYPlotter("myCanvas1"); plotter.transformXY(); let xMax = plotter.xMax; let yMax = plotter.yMax; let xMin = plotter.xMin; let yMin = plotter.yMin; plotter.plotLine(xMin, f(xMin), xMax, f(xMax), "black"); // Create Random XY Points let numPoints = 500; let xPoints = []; let yPoints = []; for (let i = 0; i < numPoints; i++) { xPoints[i] = Math.random() * xMax; yPoints[i] = Math.random() * yMax; } // Compute Desired Answers let desired = []; for (let i = 0; i < numPoints; i++) { let x = xPoints[i]; let y = yPoints[i]; let answer = 0; if (y > f(x)) {answer = 1;} desired[i] = answer; } // Create a Perceptron let ptron = new Perceptron(2); plotter.plotPoints(numPoints, xPoints, yPoints, "blue"); // Demo Function function demo(id) { let count = 0; let obj = ptron; let myTimer1 = setInterval(function(){ let x = xPoints[count]; let y = yPoints[count]; let error = ptron.train([x,y], desired[count]);; let color = "black"; if (desired[count] == 0) color = "red"; plotter.plotPoint(x, y, color); if (count == numPoints-1) { clearInterval(myTimer1); } count++; document.getElementById(id).innerHTML = "Training Points: " + count; },10,obj); } function myFunction() { demo("demo1"); document.getElementById("myButton1").style="display:none"; }