-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork_Spam.m
More file actions
67 lines (47 loc) · 1.44 KB
/
NeuralNetwork_Spam.m
File metadata and controls
67 lines (47 loc) · 1.44 KB
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
function NeuralNetwork()
input = [1 1 0 0 0 0 0 0 0;
1 0 1 0 0 0 0 0 0;
1 0 0 1 0 0 0 0 0;
1 0 0 0 1 0 0 0 0;
1 0 0 0 0 1 0 0 0;
1 0 0 0 0 0 1 0 0;
1 0 0 0 0 0 0 1 0;
1 0 0 0 0 0 0 0 1];
output = input;
hiddenvalue = [0.89 0.04 0.08;
0.15 0.99 0.99;
0.01 0.97 0.27;
0.99 0.97 0.71;
0.03 0.05 0.02;
0.01 0.11 0.88;
0.80 0.01 0.98;
0.60 0.94 0.01];
theta1 = rand(9,4);
theta2 = rand(4,9);
disp(input);
lambda = 0.5;
for i = 1:20000
layer1 = calculatePredict(input,theta1);
predict = calculatePredict(layer1.h,theta2);
bplayer = calculateTheta(output, predict.h, lambda,theta1,input,theta2, layer1.h);
theta1 = bplayer.theta1;
theta2 = bplayer.theta2;
end
%layer1 = calculatePredict(input,theta1);
%predict = calculatePredict(layer1.h,theta2);
disp(theta1);
disp(theta2);
disp(predict.h);
fprintf(1,'\n\nEnd\n');
function pre = calculatePredict(input,theta)
pre.z = input*theta;
pre.h = 1./(1+exp(1).^(-pre.z));
function th = calculateTheta(output, pre,lambda,theta1,input,theta2,pre2)
% calculate the error of output
Err = pre.*(1-pre).*(output-pre);
% calculate the error of hidden layer
sum = (Err*theta2');
Err_hidden = pre2.*(1-pre2).*sum;
% update theta
th.theta1 = theta1 + (lambda * input' * Err_hidden );
th.theta2 = theta2 + (lambda * pre2' * Err );