Skip to content

Commit

Permalink
Merge pull request #8 from ratiopharm88/master
Browse files Browse the repository at this point in the history
I tried doing the NN stuff today
  • Loading branch information
Thomas Jespersen committed Mar 18, 2012
2 parents 0d499c4 + 7c84992 commit b888c44
Show file tree
Hide file tree
Showing 4 changed files with 652 additions and 0 deletions.
44 changes: 44 additions & 0 deletions handin3/Code/NNMarco/backprop.m
@@ -0,0 +1,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
43 changes: 43 additions & 0 deletions handin3/Code/NNMarco/nn.m
@@ -0,0 +1,43 @@
function [z a] = nn(w, layers, inputs)
layernos=cumsum(layers);

%% set z0 to 1
%% z must be treated as zero-based
z=ones(1, size(w,2));
a=zeros(1, size(w,2));

%% input equals output for input neurons
for i=1:layers(1)
z(i+1)=inputs(i);
end

%% for each hidden and output neuron
%% i is the neuron number
for i=(layernos(1)+1):size(w,1)
sum=0;
first=1;
%% find first neuron of this layer
for j=2:length(layernos)
if i<=layernos(j)
first=layernos(j-1)+1;
end
end

%% iterate over all input neurons

for j=0:(first-1)
sum=sum+z(j+1)*w(i, j+1);
end

a(i+1)=sum;

%% for output neurons, output is linear, otherwise use the function.
if (i<=layernos(length(layernos)-1))
z(i+1)=sum/(1+abs(sum));
else
z(i+1)=sum;
end
end


end

0 comments on commit b888c44

Please sign in to comment.