UNPKG

nntsreinforcement

Version:

A Neural Network for Reinforcement Learning in TypeScript

67 lines (66 loc) 2.46 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Neuron = void 0; var Neuron = /** @class */ (function () { function Neuron(bias, weights, type) { if (bias === void 0) { bias = Math.random() * 2 - 1; } if (type === void 0) { type = 'Type not defined'; } this.type = type; this.activation = 0; this.bias = 0; this.weights = []; this.prevNeurons = []; this.bias = bias; if (weights != undefined) { this.weights = weights; } } Neuron.prototype.initRandomConnectionsFromLayer = function (prevLayer, connections) { this.prevNeurons = prevLayer.neurons; for (var _i = 0, _a = prevLayer.neurons; _i < _a.length; _i++) { var neuron = _a[_i]; this.weights.push(Math.random() * 2 - 1); } }; Neuron.prototype.compute = function (activations) { var sum = -this.bias; for (var i = 0; i < activations.length; i++) { sum += this.weights[i] * activations[i]; } this.activation = this.relu(sum); return this.activation; }; Neuron.prototype.binaryActivation = function (sum) { return sum < this.bias ? 0 : 1; }; Neuron.prototype.sigmoid = function (a) { return 1 / (1 + Math.pow(Math.E, -a)); }; Neuron.prototype.relu = function (a) { return a > 0 ? a : 0; }; Neuron.prototype.mutate = function (rate) { this.bias = this.linearInterpolation(this.bias, Math.random() * 2 - 1, rate); for (var i = 0; i < this.weights.length; i++) { this.weights[i] = this.linearInterpolation(this.weights[i], Math.random() * 2 - 1, rate); } }; Neuron.prototype.deepCopy = function () { var newWeights = __spreadArray([], this.weights, true); return new Neuron(this.bias, newWeights, this.type); }; Neuron.prototype.linearInterpolation = function (a, b, d) { return a + (b - a) * d; }; return Neuron; }()); exports.Neuron = Neuron;