forked from laumann/statml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backprop.m
44 lines (34 loc) · 1.07 KB
/
backprop.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
function [delta der] = backprop (w, z, a, targety, layers)
layersum=cumsum(layers);
%delta=zeros(1, layersum(length(layersum))-layersum(1));
delta=zeros(1, layersum(length(layersum)));
der=zeros(size(w));
%for i=layersum(length(layersum)):-1:(layersum(1)+1)
for i=layersum(length(layersum)):-1:1
%% if it is an output neuron
if (i>layersum(length(layersum)-1))
%delta(i-layersum(1)) = z(i+1)-targety(i-layersum(length(layersum)-1));
delta(i) = z(i+1)-targety(i-layersum(length(layersum)-1));
else
ai=a(i+1);
ha=1/((1+abs(ai))^2);
sum=0;
for k=i+1:layersum(length(layersum))
sum=sum+delta(k)*w(k,i+1);
end
delta(i) = ha * sum;
end
end
%% compute the partial derivatives
for i=1:1:layersum(length(layersum))
% if i<layersum(1)+1
% for j=0:1:layersum(length(layersum))
% der(i,j+1)=delta(i-layersum(1)) * z(j+1);
% end
% else
for j=0:1:layersum(length(layersum))
der(i,j+1)=delta(i) * z(j+1);
end
% end
end
end