-
Notifications
You must be signed in to change notification settings - Fork 2
/
DBN.m
100 lines (81 loc) · 3.02 KB
/
DBN.m
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
classdef DBN < handle
%DBN Summary of this class goes here
% Detailed explanation goes here
properties
inputlayer = 0
layers = {}
compiled = false
end
methods
function dbn = DBN()
end
function add(dbn, layer)
if ~isa(layer, 'BernoulliRBM') && ~isa(layer, 'GaussianBernoulliRBM')
throw(MException('DBN:notAValidLayer', ...
'Not a valid layer.'));
end
dbn.layers{end+1} = layer;
end
function compile(dbn)
if length(dbn.layers) <= 0
throw(MException('DBN:noLayers', ...
'The model does not contain any layer.'));
end
for i = 1:length(dbn.layers)
% Register the input layer
if i == 1
dbn.inputlayer = dbn.layers{1};
end
% Check for the match of dimensions
if i ~= length(dbn.layers)
assert(dbn.layers{i}.hiddenunits == dbn.layers{i+1}.visibleunits);
end
% The hidden layers and output layer
dbn.layers{i}.init();
end
dbn.compiled = true;
end
function fit(dbn, X, options)
for i = 1:length(dbn.layers)
fprintf('[DBN] Fitting RBM: %d / %d\n', i, length(dbn.layers));
dbn.layers{i}.fit(X, options);
X = dbn.layers{i}.rbmup(X);
end
end
function X = evaluate(dbn, X)
% Forward pass
for i = 1:length(dbn.layers)
X = dbn.layers{i}.rbmup(X);
end
% Backward pass
for i = length(dbn.layers):-1:1
X = dbn.layers{i}.rbmdown(X);
end
end
function model = tosequential(dbn, options, outputlayer)
n = length(dbn.layers);
assert(n > 0);
assert(options{1} == 0);
assert(n + 1 == length(options));
model = SequentialModel();
% Input layer
model.add(InputLayer(dbn.layers{1}.visibleunits));
% Hidden layers
for i = 2:n
model.add(DenseLayer(dbn.layers{i}.visibleunits, options{i}));
end
model.add(DenseLayer(dbn.layers{n}.hiddenunits, options{n+1}));
% Output layer
model.add(outputlayer);
% Compile the model
model.compile();
% Copy variables
for i = 1:length(dbn.layers)
% i is the i-th layer in the DBN and the 'i+1'-th layer in
% the SequentialModel
model.layers{i+1}.W = dbn.layers{i}.W;
model.layers{i+1}.b = dbn.layers{i}.c;
end
end
end
end