Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ratiopharm88/master
I tried doing the NN stuff today
- Loading branch information
Showing
4 changed files
with
652 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.