Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- var Perceptron = {
- modelFunction: function (modelledFunction) {
- for (var i = 0; i < 40; i++) {
- this.runEra(modelledFunction, true);
- }
- },
- test: function (modelledFunction) {
- this.runEra(modelledFunction, false);
- },
- runEra: function (modelledFunction, correctionNeed) {
- var eraTable = EraOutputter.createBlankEraTable();
- this.sumError = 0;
- for (var stepIndex = 0; stepIndex < Math.pow(2, this.modelledFunctionArity); stepIndex++) {
- var binaryInputs = this.numberToBinaryInputs(stepIndex);
- var target = modelledFunction(binaryInputs);
- this.elementaryStep(eraTable, binaryInputs, target, correctionNeed);
- }
- EraOutputter.outputEra(eraTable, this.sumError);
- },
- elementaryStep: function (table, binaryInputs, target, correctionNeed) {
- var netResult = this.netResult(binaryInputs);
- EraOutputter.addRow(table, binaryInputs, target, this.weights, netResult);
- this.sumError += Math.abs(netResult - target);
- if (correctionNeed) {
- this.weights[0] += this.educationRate * (target - netResult) * 1;
- for (var i = 1; i < this.weights.length; i++) {
- this.weights[i] += this.educationRate * (target - netResult) * (binaryInputs[i - 1]);
- }
- }
- },
- netResult: function (binaryInputs) {
- var net = this.weights[0];
- for (var i = 1; i < this.weights.length; i++) {
- net += this.weights[i] * binaryInputs[i - 1];
- }
- return (net >= 0) ? 1 : 0;
- },
- weights: [0, 0, 0, 0, 0],
- modelledFunctionArity: 4,
- educationRate: 1,
- sumError: 0,
- numberToBinaryInputs: function (number) {
- var binaryString = number.toString(2).padStart(4, "0");
- var binaryInputs = [];
- for (var i = 0; i < this.modelledFunctionArity; i++) {
- binaryInputs.push(+binaryString[i]);
- }
- return binaryInputs;
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement