-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
101 lines (90 loc) · 2.33 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var RBM = {};
RBM.Create = function(inDimensionsIn, inDimensionsOut)
{
var obj = {};
var min = [];
var max = [];
var i;
inDimensionsIn++;
inDimensionsOut++;
for(i=0; i<inDimensionsIn; i++)
{
min.push(-0.1);
max.push(0.1);
}
obj.MatrixForward = M.Box([min, max], inDimensionsOut);
obj.MatrixBackward = M.Transpose(obj.MatrixForward);
return obj;
};
RBM.Bool = function(inData)
{
var i, j;
for(i=0; i<inData.length; i++)
{
for(j=0; j<inData[i].length; j++)
{
var rand = Math.random();
if(inData[i][j] > rand)
inData[i][j] = 1;
else
inData[i][j] = 0;
}
}
return inData;
};
RBM.Out = function(inRBM, inData)
{
// inData MUST be padded before calling this method
return M.Repad( M.Sigmoid(M.Transform(inRBM.MatrixForward, inData)) );
};
RBM.Back = function(inRBM, inData)
{
// inData MUST be padded before calling this method
return M.Repad( M.Sigmoid(M.Transform(inRBM.MatrixBackward, inData)) );
};
// contrative divergence
RBM.CD = function(inRBM, inData, inCDN, inRate)
{
var pos;
var neg;
var i;
var initial, current;
var current = RBM.Out(inRBM, inData);
var initial = RBM.Bool(M.Clone(current));
for(i=0; i<inCDN; i++)
{
current = RBM.Out(inRBM, RBM.Back(inRBM, current));
}
for(i=0; i<inData.length; i++)
{
pos = M.Outer(inData[i], initial[i]);
neg = M.Outer(inData[i], current[i]);
inRBM.MatrixForward = M.Add(inRBM.MatrixForward, M.Scale(pos, inRate));
inRBM.MatrixForward = M.Subtract(inRBM.MatrixForward, M.Scale(neg, inRate));
}
inRBM.MatrixBackward = M.Transpose(inRBM.MatrixForward);
};
RBM.Train = function(inRBM, inData, inIterations, inCDN, inRate)
{
var i;
var copy = M.Pad(M.Clone(inData));
for(i=0; i<inIterations; i++)
{
RBM.CD(inRBM, copy, inCDN, inRate)
}
};
RBM.Observe = function(inRBM, inData, inIterations)
{
var i;
var obs = M.Pad(M.Clone(inData));
for(i=0; i<inIterations; i++)
{
obs = RBM.Back( inRBM, RBM.Bool(RBM.Out(inRBM, obs)) );
}
return M.Unpad(obs);
};
RBM.Label = function(inRBM, inData)
{
var i, j;
return M.Unpad(RBM.Out(inRBM, M.Pad(M.Clone(inData))));
};