Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. var Perceptron = {
  3.     modelFunction: function (modelledFunction) {
  4.         for (var i = 0; i < 40; i++) {
  5.             this.runEra(modelledFunction, true);
  6.         }
  7.     },
  8.     test: function (modelledFunction) {
  9.         this.runEra(modelledFunction, false);
  10.     },
  11.     runEra: function (modelledFunction, correctionNeed) {
  12.         var eraTable = EraOutputter.createBlankEraTable();
  13.         this.sumError = 0;
  14.  
  15.         for (var stepIndex = 0; stepIndex < Math.pow(2, this.modelledFunctionArity); stepIndex++) {
  16.             var binaryInputs = this.numberToBinaryInputs(stepIndex);
  17.             var target = modelledFunction(binaryInputs);
  18.             this.elementaryStep(eraTable, binaryInputs, target, correctionNeed);
  19.         }
  20.         EraOutputter.outputEra(eraTable, this.sumError);
  21.     },
  22.     elementaryStep: function (table, binaryInputs, target, correctionNeed) {
  23.         var netResult = this.netResult(binaryInputs);
  24.         EraOutputter.addRow(table, binaryInputs, target, this.weights, netResult);
  25.  
  26.         this.sumError += Math.abs(netResult - target);
  27.         if (correctionNeed) {
  28.             this.weights[0] += this.educationRate * (target - netResult) * 1;
  29.             for (var i = 1; i < this.weights.length; i++) {
  30.                 this.weights[i] += this.educationRate * (target - netResult) * (binaryInputs[i - 1]);
  31.             }
  32.         }
  33.     },
  34.     netResult: function (binaryInputs) {
  35.         var net = this.weights[0];
  36.         for (var i = 1; i < this.weights.length; i++) {
  37.             net += this.weights[i] * binaryInputs[i - 1];
  38.         }
  39.         return (net >= 0) ? 1 : 0;
  40.     },
  41.     weights: [0, 0, 0, 0, 0],
  42.     modelledFunctionArity: 4,
  43.     educationRate: 1,
  44.     sumError: 0,
  45.  
  46.     numberToBinaryInputs: function (number) {
  47.         var binaryString = number.toString(2).padStart(4, "0");
  48.         var binaryInputs = [];
  49.         for (var i = 0; i < this.modelledFunctionArity; i++) {
  50.             binaryInputs.push(+binaryString[i]);
  51.         }
  52.         return binaryInputs;
  53.     },
  54. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement