neuralnetwork
Version:
Rudimentary Neural Network in Typescript
43 lines (41 loc) • 2.11 kB
JavaScript
/*globals module, require*/
"use strict";
var utils_1 = require('./utils');
/**
* Connects 2 layers together. Contains the parameters of the network
*/
var FullConnection = (function () {
function FullConnection(inputLayer, outputLayer) {
this.parameters = [];
this.derivatives = [];
for (var i = 0; i < inputLayer.neurons.length * outputLayer.neurons.length; i += 1) {
this.parameters[i] = (Math.random() > 0.5 ? 1 : -1) * 2 * Math.random();
this.derivatives[i] = 0;
}
this.inputLayer = inputLayer;
this.outputLayer = outputLayer;
}
FullConnection.prototype.resetParameters = function (val) {
this.parameters = this.parameters.map(function (el) {
return val || 0;
});
};
FullConnection.prototype.resetDerivatives = function () {
utils_1.zero(this.derivatives);
};
FullConnection.prototype.forward = function () {
this.outputLayer.inputBuffer = utils_1.vectorSum(this.outputLayer.inputBuffer, utils_1.matrixVectorMultiplication(this.parameters, this.inputLayer.outputBuffer));
};
FullConnection.prototype.backward = function () {
var self = this, prevActivationVector = this.inputLayer.outputBuffer, parametersTransposed = utils_1.transpose(this.parameters, this.inputLayer.outputError.length), scaledErrors = utils_1.matrixVectorMultiplication(parametersTransposed, this.outputLayer.inputError);
// The error multiplied by the parameter (aka weight)
this.inputLayer.outputError = utils_1.vectorSum(this.inputLayer.outputError, scaledErrors);
this.derivatives = utils_1.outerProduct(this.outputLayer.inputError, this.inputLayer.outputBuffer);
//console.log('Connection backward > deriv', this.derivatives);
//console.log('Connection backward > in.outBuf', this.inputLayer.outputBuffer);
//console.log('Connection backward > out.inErr', this.outputLayer.inputError);
};
return FullConnection;
}());
exports.FullConnection = FullConnection;
//# sourceMappingURL=../maps/lib/connections.js.map