1 |
|
2 |
|
3 |
|
4 | var math = require('./utils').math;
|
5 | LogisticRegression = module.exports = function (settings) {
|
6 | var self = this;
|
7 | self.x = settings['input'];
|
8 | self.y = settings['label'];
|
9 | self.W = math.zeroMat(settings['n_in'],settings['n_out']);
|
10 | self.b = math.zeroVec(settings['n_out']);
|
11 | self.settings = {
|
12 | 'log level' : 1
|
13 | };
|
14 | };
|
15 |
|
16 | LogisticRegression.prototype.train = function (settings) {
|
17 | var self = this;
|
18 | var lr = 0.1, epochs = 200;
|
19 | if(typeof settings['input'] !== 'undefined')
|
20 | self.x = settings['input'];
|
21 | if(typeof settings['lr'] !== 'undefined')
|
22 | lr = settings['lr'];
|
23 | if(typeof settings['epochs'] !== 'undefined')
|
24 | epochs = settings['epochs'];
|
25 | var i;
|
26 | var currentProgress = 1;
|
27 | for(i=0;i<epochs;i++) {
|
28 | var probYgivenX = math.softmaxMat(math.addMatVec(math.mulMat(self.x,self.W),self.b));
|
29 | var deltaY = math.minusMat(self.y,probYgivenX);
|
30 |
|
31 | var deltaW = math.mulMat(math.transpose(self.x),deltaY);
|
32 | var deltaB = math.meanMatAxis(deltaY,0);
|
33 |
|
34 | self.W = math.addMat(self.W,math.mulMatScalar(deltaW,lr));
|
35 | self.b = math.addVec(self.b,math.mulVecScalar(deltaB,lr));
|
36 | if(self.settings['log level'] > 0) {
|
37 | var progress = (1.*i/epochs)*100;
|
38 | if(progress > currentProgress) {
|
39 | console.log("LogisticRegression",progress.toFixed(0),"% Completed.");
|
40 | currentProgress++;
|
41 | }
|
42 | }
|
43 | }
|
44 | if(self.settings['log level'] > 0)
|
45 | console.log("LogisticRegression Final Cross Entropy : ",self.getReconstructionCrossEntropy());
|
46 | };
|
47 |
|
48 | LogisticRegression.prototype.getReconstructionCrossEntropy = function () {
|
49 | var self = this;
|
50 | var probYgivenX = math.softmaxMat(math.addMatVec(math.mulMat(self.x,self.W),self.b));
|
51 | var a = math.mulMatElementWise(self.y, math.activateMat(probYgivenX,Math.log));
|
52 | var b = math.mulMatElementWise(math.mulMatScalar(math.addMatScalar(self.y,-1),-1),
|
53 | math.activateMat(math.mulMatScalar(math.addMatScalar(probYgivenX,-1),-1),Math.log));
|
54 | var crossEntropy = -math.meanVec(math.sumMatAxis(math.addMat(a,b),1));
|
55 | return crossEntropy;
|
56 | };
|
57 |
|
58 | LogisticRegression.prototype.predict = function (x) {
|
59 | var self = this;
|
60 | return math.softmaxMat(math.addMatVec(math.mulMat(x,self.W),self.b));
|
61 | };
|
62 |
|
63 | LogisticRegression.prototype.set = function(property,value) {
|
64 | var self = this;
|
65 | self.settings[property] = value;
|
66 | } |
\ | No newline at end of file |