UNPKG

1.77 kBJavaScriptView Raw
1/**
2 * Created by joonkukang on 2014. 1. 12..
3 */
4var math = require('./utils').math;
5HiddenLayer = module.exports = function (settings) {
6 var self = this;
7 self.input = settings['input'];
8
9 if(typeof settings['W'] === 'undefined') {
10 var a = 1. / settings['n_in'];
11 settings['W'] = math.randMat(settings['n_in'],settings['n_out'],-a,a);
12 }
13 if(typeof settings['b'] === 'undefined')
14 settings['b'] = math.zeroVec(settings['n_out']);
15 if(typeof settings['activation'] === 'undefined')
16 settings['activation'] = math.sigmoid;
17
18 self.W = settings['W'];
19 self.b = settings['b'];
20 self.activation = settings['activation'];
21}
22
23HiddenLayer.prototype.output = function(input) {
24 var self = this;
25 if(typeof input !== 'undefined')
26 self.input = input;
27
28 var linearOutput = math.addMatVec(math.mulMat(self.input,self.W),self.b);
29 return math.activateMat(linearOutput,self.activation);
30};
31
32HiddenLayer.prototype.linearOutput = function(input) { // returns the value before activation.
33 var self = this;
34 if(typeof input !== 'undefined')
35 self.input = input;
36
37 var linearOutput = math.addMatVec(math.mulMat(self.input,self.W),self.b);
38 return linearOutput;
39}
40
41HiddenLayer.prototype.backPropagate = function (input) { // example+num * n_out matrix
42 var self = this;
43 if(typeof input === 'undefined')
44 throw new Error("No BackPropagation Input.")
45
46 var linearOutput = math.mulMat(input, m.transpose(self.W));
47 return linearOutput;
48}
49
50HiddenLayer.prototype.sampleHgivenV = function(input) {
51 var self = this;
52 if(typeof input !== 'undefined')
53 self.input = input;
54
55 var hMean = self.output();
56 var hSample = math.probToBinaryMat(hMean);
57 return hSample;
58}
\No newline at end of file