UNPKG

neuralnetwork

Version:

Rudimentary Neural Network in Typescript

78 lines (76 loc) 2.44 kB
//var key; // Temp variable for copying keys in a loop "use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; /** * Neural models for layers. * Should contain a name and both forward and backward interfaces */ var Neuron = (function () { function Neuron() { this.name = Math.random().toString(36).substring(2); } Neuron.prototype.forward = function (input) { return input; }; Neuron.prototype.backward = function (error, ouput) { return error; }; return Neuron; }()); exports.Neuron = Neuron; var IdentityNeuron = (function (_super) { __extends(IdentityNeuron, _super); function IdentityNeuron() { _super.apply(this, arguments); } return IdentityNeuron; }(Neuron)); exports.IdentityNeuron = IdentityNeuron; var BiasNeuron = (function (_super) { __extends(BiasNeuron, _super); function BiasNeuron() { _super.apply(this, arguments); } BiasNeuron.prototype.forward = function (input) { return 1; }; BiasNeuron.prototype.backward = function (error) { return 0; }; return BiasNeuron; }(Neuron)); exports.BiasNeuron = BiasNeuron; var SigmoidNeuron = (function (_super) { __extends(SigmoidNeuron, _super); function SigmoidNeuron() { _super.apply(this, arguments); } SigmoidNeuron.prototype.forward = function (input) { return 1.0 / (1.0 + Math.exp(-input)); }; // Output is the activation value created by forward SigmoidNeuron.prototype.backward = function (err, output) { return output * (1 - output) * err; }; return SigmoidNeuron; }(Neuron)); exports.SigmoidNeuron = SigmoidNeuron; var RectifiedLinearNeuron = (function (_super) { __extends(RectifiedLinearNeuron, _super); function RectifiedLinearNeuron() { _super.apply(this, arguments); } RectifiedLinearNeuron.prototype.forward = function (input) { return Math.max(0, input); }; RectifiedLinearNeuron.prototype.backward = function (err, output) { return ((output > 0) ? err : 0); }; return RectifiedLinearNeuron; }(Neuron)); exports.RectifiedLinearNeuron = RectifiedLinearNeuron; //# sourceMappingURL=../maps/lib/neurons.js.map