UNPKG

rlab

Version:

Javascript scientific library like R

71 lines (59 loc) 2.4 kB
/** * Created by joonkukang on 2014. 1. 12.. */ var math = require('./utils').math; var M = R.M; HiddenLayer = module.exports = function (settings) { var self = this; self.input = settings['input']; if(typeof settings['W'] === 'undefined') { var a = 1. / settings['n_in']; // settings['W'] = math.randMat(settings['n_in'],settings['n_out'],-a,a); settings['W'] = M.randomM(settings['n_in'],settings['n_out'],-a,a); } if(typeof settings['b'] === 'undefined') // settings['b'] = math.zeroVec(settings['n_out']); settings['b'] = M.newV(settings['n_out']); if(typeof settings['activation'] === 'undefined') // settings['activation'] = math.sigmoid; settings['activation'] = R.NN.sigmoid; self.W = settings['W']; self.b = settings['b']; self.activation = settings['activation']; } HiddenLayer.prototype.output = function(input) { var self = this; if(typeof input !== 'undefined') self.input = input; // var linearOutput = math.addMatVec(math.mulMat(self.input,self.W),self.b); // return math.activateMat(this.linearOutput(input),self.activation); return this.linearOutput(input).map1(this.activation); }; HiddenLayer.prototype.linearOutput = function(input) { // returns the value before activation. var self = this; if(typeof input !== 'undefined') self.input = input; // var linearOutput = math.addMatVec(math.mulMat(self.input,self.W),self.b); var linearOutput = this.input.dot(this.W).addMV(this.b); // var linearOutput = R.addMV(R.mdot(this.input, this.W), this.b); // var linearOutput = R.addMV(math.mulMat(this.input, this.W), this.b); return linearOutput; } HiddenLayer.prototype.backPropagate = function (input) { // example+num * n_out matrix var self = this; if(typeof input === 'undefined') throw new Error("No BackPropagation Input.") // var linearOutput = math.mulMat(input, m.transpose(self.W)); var linearOutput = input.dot(this.W.tr()); return linearOutput; } HiddenLayer.prototype.sampleHgivenV = function(input) { var self = this; if(typeof input !== 'undefined') self.input = input; var hMean = self.output(); // var hSample = math.probToBinaryMat(hMean); var hSample = R.NN.binarySample(hMean); // 有亂數 // R.be("sampleHgivenV", R.eq(hSample, hSample2)); return hSample; }