UNPKG

1.84 kBJavaScriptView Raw
1/**
2 * Created by joonkukang on 2014. 1. 13..
3 */
4
5var math = require('./utils').math;
6RBM = require('./RBM');
7
8CRBM = module.exports = function (settings) {
9 RBM.call(this,settings);
10};
11
12CRBM.prototype = new RBM({});
13
14CRBM.prototype.propdown = function(h) {
15 var self = this;
16 var preSigmoidActivation = math.addMatVec(math.mulMat(h,math.transpose(self.W)),self.vbias);
17 return preSigmoidActivation;
18};
19
20CRBM.prototype.sampleVgivenH = function(h0_sample) {
21 var self = this;
22 var a_h = self.propdown(h0_sample);
23 var a = math.activateMat(a_h,function(x) { return 1. / (1-Math.exp(-x)) ; });
24 var b = math.activateMat(a_h,function(x){ return 1./x ;});
25 var v1_mean = math.minusMat(a,b);
26 var U = math.randMat(math.shape(v1_mean)[0],math.shape(v1_mean)[1],0,1);
27 var c = math.activateMat(a_h,function(x) { return 1 - Math.exp(x);});
28 var d = math.activateMat(math.mulMatElementWise(U,c),function(x) {return 1-x;});
29 var v1_sample = math.activateTwoMat(math.activateMat(d,Math.log),a_h,function(x,y) {
30 if(y==0) y += 1e-14; // Javascript Float Precision Problem.. This is a limit of javascript.
31 return x/y;
32 })
33 return [v1_mean,v1_sample];
34};
35CRBM.prototype.getReconstructionCrossEntropy = function() {
36 var self = this;
37 var reconstructedV = self.reconstruct(self.input);
38 var a = math.activateTwoMat(self.input,reconstructedV,function(x,y){
39 return x*Math.log(y);
40 });
41
42 var b = math.activateTwoMat(self.input,reconstructedV,function(x,y){
43 return (1-x)*Math.log(1-y);
44 });
45
46 var crossEntropy = -math.meanVec(math.sumMatAxis(math.addMat(a,b),1));
47 return crossEntropy;
48
49};
50
51CRBM.prototype.reconstruct = function(v) {
52 var self = this;
53 var reconstructedV = self.sampleVgivenH(self.sampleHgivenV(v)[0])[0];
54 return reconstructedV;
55};
\No newline at end of file