-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoy.m
More file actions
198 lines (152 loc) · 3.88 KB
/
toy.m
File metadata and controls
198 lines (152 loc) · 3.88 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
function [] = toy()
im = load('newimages.mat');
l = load('newlabels.mat');
images = im.images;
labels = l.labels;
%logistic_regression(images, labels, 0.000001);
images = [1 1 0 1]';
labels = [1];
neural_net(images, labels, 1);
end
function [] = logistic_regression(x, t, eta)
[K, ~] = size(t);
[M, N] = size(x);
w = zeros(K, M);
for iter=1:70
for i=1:N
phi = x(:,i);
tn = t(:,i);
an = w*phi; %10x1
yn = softmax_activation(an);
for k=1:K
deltaE = (yn(k) - tn(k))*phi;
w(k, :) = w(k, :) - eta*deltaE';
end
end
end
res = w*x;
result = softmax_activation(res);
[~, class] = max(result);
count = 0;
l=load('labels.mat');
labels = l.labels;
for i=1:N
if class(i)== labels(i)
count=count+1;
end
if class(i)-labels(i)==10
count=count+1;
class(i) = 0;
end
end
count
save('weight.mat', 'w', 'result', 'class');
end
function [] = neural_net(images, t, eta)
eta=0.9;
l=load('labels.mat');
labels = l.labels;
x=images;
%{
for i=images
temp1 = reshape(i(2:length(i)), 28, 28);
temp2 = [sum(temp1) sum(temp1')];
x = [x [1 temp2]'];
end
%}
[K, ~] = size(t);
[M, N] = size(x);
J = 2; %Number of units in hidden layer
w1 = ones(J, M);
w2 = ones(K, J+1);
w1 = [-0.4 0.2 0.4 -0.5;
0.2 -0.3 0.1 0.2];
w2 = [0.1 -0.3 -0.2];
for iter=1:1
for i=1:N
phi = x(:,i);
tn = t(:,i);
labels(i);
bn = w1*phi; %Jx1
z=[1; h(bn)];
an = w2*z;
yn = sigmoid(an);
sigmak = compute_sigmak(yn, tn);
sigmaj = compute_sigmaj(z, w2, sigmak);
w2 = update_w2(w2, sigmak, z, eta);
w1 = update_w1(w1, sigmaj(2:J+1), phi, eta);
end
end
w1
w2
%w1
%w2
count=0;
[res, class] = NN_classify(x, w1, w2);
for i=1:length(class)
if class(i) == labels(i)
count=count+1;
end
end
count
save('nn_weights.mat', 'w1', 'w2', 'class', 'res');
end
function [yn] = softmax_activation(an)
[a,b] = size(an);
yn = zeros(a,b);
denom = sum(exp(an));
exp(an(:,1))./denom(1);
for i=1:b
yn(:,i) = exp(an(:,i))./denom(i);
end
end
function [res] = h(bn)
res = sigmoid(bn);
%res = relu(bn);
end
function [res] = h_prime(zn)
res = diff_sigmoid(zn);
%res = diff_relu(zn);
end
function [res] = relu(a)
res = arrayfun(@(x) max(0, x), a);
end
function [res] = diff_relu(a)
res = arrayfun(@(x) x>0, a);
end
function [res] = sigmoid(a)
res = arrayfun(@(x) 1/(1+exp(-1*x)), a);
end
function [res] = diff_sigmoid(z)
res = arrayfun(@(x) (1/(1+exp(-1*x)))*(1 - (1/(1+exp(-1*x)))), z);
end
function [res] = compute_sigmak(yn, tn)
res = yn*(1-yn)*(yn - tn);
end
function [res] = compute_sigmaj(z, w2, sigmak)
[J, ~] = size(z);
res = zeros(J, 1);
for j=1:J
weighted_error = sum(w2(j)' * sigmak);
res(j) = z(j)*(1-z(j))* weighted_error;
end
end
function [updated_w1] = update_w1(w1, sigmaj, phi, eta)
dw1 = sigmaj*phi';
updated_w1 = w1 - eta*dw1;
end
function [updated_w2] = update_w2(w2, sigmak, z, eta)
dw2 = sigmak*z';
updated_w2 = w2 - eta*dw2;
end
function [res, class_result] = NN_classify(x, w1, w2)
[~, N] = size(x);
z = [ones(1, N); h(w1*x)]; %hidden layer
res = softmax_activation(w2*z);
[~, class_result] = max(res);
for i=1:length(class_result)
if class_result(i)==10
class_result(i) = 0;
end
end
end