-
Notifications
You must be signed in to change notification settings - Fork 0
/
auf82.m
122 lines (100 loc) · 2.23 KB
/
auf82.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
clear all;
close all;
clc;
more off;
% Daten aus Dateien einlesen
X_train=dlmread('pendigits-training.txt');
X_test=dlmread('pendigits-testing.txt');
y_train = X_train(:,end);
t_train = zeros(size(y_train,1),10);
for i=1:size(y_train)
t_train(i,y_train(i) + 1) = 1;
end
y_test = X_test(:,end);
X_train = X_train(:,1:16);
X_test = X_test(:,1:16);
% Zentriere Daten
u_train = mean(X_train,1);
u_test = mean(X_test,1);
X_train = X_train - repmat(u_train,size(X_train,1),1);
X_test = X_test - repmat(u_test,size(X_test,1),1);
% Subsetgröße
s = 100;
% Anzahl innerer Neuronen
n = 15;
% Fehlerschwelle
epsilon = .02*s;
u = 1.2;
d = 0.5;
gammamax = 50;
gammamin = 10^-6;
% random weights
%W1_hat = (rand(17,n).-.5)*100;
%W2_hat = (rand(n+1,10).-.5)*100;
W1_hat = rand(17,n);
W2_hat = rand(n+1,10);
gamma1 = ones(17,n)*0.1;
gamma2 = ones(n+1,10)*0.1;
oldgrad1 = zeros(17,n);
oldgrad2 = zeros(n+1,10);
count = 0;
do
count += 1;
E = 0;
grad1 = 0;
grad2 = 0;
for(j=1:s)
% Input
%i = randi(size(X_train,1));
[g1,g2,error] = backprop(X_train(j,:),W1_hat,W2_hat,t_train(j,:));
E += error;
grad1 += g1;
grad2 += g2;
endfor
newgrad1 = grad1;
newgrad2 = grad2;
a = newgrad1 .* oldgrad1;
b = newgrad2 .* oldgrad2;
for(i=1:size(a,1))
for(j=1:size(a,2))
if(a(i,j)>0)
gamma1(i,j) = min(gamma1(i,j)*u,gammamax);
W1_hat(i,j) += -gamma1(i,j) * sign(newgrad1(i,j));
oldgrad1(i,j) = newgrad1(i,j);
elseif(a(i,j)<0)
gamma1(i,j) = max(gamma1(i,j)*d,gammamin);
oldgrad1(i,j) = 0;
else
W1_hat(i,j) += -gamma1(i,j) * sign(newgrad1(i,j));
oldgrad1(i,j) = newgrad1(i,j);
endif
end
end
for(i=1:size(b,1))
for(j=1:size(b,2))
if(b(i,j)>0)
gamma2(i,j) = min(gamma2(i,j)*u,gammamax);
W2_hat(i,j) += -gamma2(i,j) * sign(newgrad2(i,j));
oldgrad2(i,j) = newgrad2(i,j);
elseif(b(i,j)<0)
gamma2(i,j) = max(gamma2(i,j)*d,gammamin);
oldgrad2(i,j) = 0;
else
W2_hat(i,j) += -gamma2(i,j) * sign(newgrad2(i,j));
oldgrad2(i,j) = newgrad2(i,j);
endif
end
end
E
Es(count,1) = E;
until(E < epsilon)
plot(Es);
hits = 0;
datasize = size(X_test,1);
for(i=1:datasize)
class = classify(W1_hat,W2_hat,X_test(i,:));
if(class == y_test(i))
hits += 1;
endif
end
ER = hits/datasize *100