%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Backpropagation program
%%
%% For problem 3, you must write
%%
%% (1) initweights
%% (2) backpropstep
%% (3) compute_output (which is needed in backpropstep anyway)
%% + some additional functions, e.g. sigma(z,c)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

k_error = 10;

c = 3.0;     % sigmoid parameter (other values are possible!)
eta = 0.30;  % training parameters (other values are possible!)

%% Example of input (for the case n=3)
nin = 3;
nhidden = 3;
nout = 1;
ndata = 8;
in = [ [0 0 0]; [0 0 1]; [0 1 0]; [0 1 1]; [1 0 0]; [1 0 1]; [1 1 0]; [1 1 1] ];
out =[      0;        1;       1;        0;        1;        0;        0;        1];

yout = zeros(ndata,1);

wih = initweights(nin,nhidden);     % Initialize weights, range [-0.25,0.25] (for example)
who = initweights(nhidden,nout);    % Initialize weights. range [-0.25,0.25] (for example)

epoch = 0;
etot = 1000;
e0 = 0.01;

while (etot > e0)

 epoch = epoch + 1;

 % Form a random permutation of the inputs
 list = randperm(ndata); 

 for ex = 1:ndata
   
   i = list(ex);
   
   % Take one backpropagation step, updating wih and who.
   [new_wih,new_who] = backpropstep(nin,nhidden,nout,wih,who,i,in,out,c,eta);
  
 
   wih = new_wih;
   who = new_who;
 end  

 % Compute the error over the whole data set, every k_error epoch:

 if (epoch == k_error*fix(epoch/k_error))
  etot = 0.0;
  for ic = 1:ndata
   yout(ic,1) = compute_output(wih,who,nin,nhidden,nout,in(ic,:),c);
   for j = 1:nout
    etot = etot + (yout(ic,1)-out(ic,1))^2;
   end
  end
  etot = sqrt(etot/ndata) 
  epoch, etot
 end  

end

etot
wih
who

