-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathmlp_classify.m
78 lines (65 loc) · 2.08 KB
/
mlp_classify.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
% mlp_classify
% Copyright (C) 2011 KyungHyun Cho, Tapani Raiko, Alexander Ilin
%
%This program is free software; you can redistribute it and/or
%modify it under the terms of the GNU General Public License
%as published by the Free Software Foundation; either version 2
%of the License, or (at your option) any later version.
%
%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%GNU General Public License for more details.
%
%You should have received a copy of the GNU General Public License
%along with this program; if not, write to the Free Software
%Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
%
function [c, posterior] = mlp_classify(M, x0, Q0, raw)
if nargin < 3
Q0 = [];
end
if nargin < 4
raw = 0;
end
layers = M.structure.layers;
n_layers = length(layers);
posterior = x0;
if isfield(M, 'dbm') && M.dbm.use
for l = 2:n_layers
if M.dropout.use && l > 2
posterior = posterior * bsxfun(@times, (M.W{l-1}), 1 - M.dropout.probs{l-1});
else
posterior = posterior * M.W{l-1};
end
if l < n_layers-1
posterior = posterior + Q0{l+1} * (M.dbm.W{l})';
end
posterior = bsxfun(@plus, posterior, M.biases{l}');
if l < n_layers
posterior = sigmoid(posterior, M.hidden.use_tanh);
end
if l == n_layers && M.output.binary
posterior = softmax(posterior);
end
end
else
for l = 2:n_layers
if M.dropout.use && l > 2
posterior = bsxfun(@plus, posterior * (M.W{l-1}/2), M.biases{l}');
else
posterior = bsxfun(@plus, posterior * M.W{l-1}, M.biases{l}');
end
if l < n_layers
posterior = sigmoid(posterior, M.hidden.use_tanh);
end
if l == n_layers && M.output.binary
posterior = softmax(posterior);
end
end
end
if raw
c = posterior;
else
[maxp, c] = max(posterior, [], 2);
end