forked from hannesvdvreken/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.m
40 lines (30 loc) · 1.07 KB
/
predict.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
% author: Hannes Van De Vreken
% license: MIT
function p = predict(X, weights, layers, input_size, layer_size, output_size)
%------------------------------------------------------------------------------------
% Prepare
%------------------------------------------------------------------------------------
% unwind
[w1 w2 w3] = unwind(weights, layers, input_size, layer_size, output_size);
%------------------------------------------------------------------------------------
% feed forward
%------------------------------------------------------------------------------------
% add bias ones
X_bias = [ones(size(X, 1), 1) X];
% feed forward
z = X_bias * w1;
a = [ones(size(z, 1), 1) sigmoid(z)];
% pre-allocate
w = zeros(size(w2, 2), size(w2, 3));
internal_layers = size(w2,1);
% loop
for idx = 1:internal_layers
% convert from 3d to 2d matrix
w = reshape(w2(idx,:,:), size(w2, 2), size(w2, 3));
z = a * w;
a = [ones(size(z, 1), 1) sigmoid(z)];
end
% neuron status of last internal layer times weights to output layer
z = a * w3;
p = sigmoid(z) > 0.5;
end