%main6b.m
%  (c) 1999 Finn Aarup Nielsen, Lars Kai Hansen
%     Dept Mathematical Modelling, Technical University of Denmark
%
%  See also: NR_TRAINX

%  cvs: 
  
  
  %%%%%%%%%%%%%%%%%%%%%
  % Initialization    %
  %%%%%%%%%%%%%%%%%%%%%
  
  Ni = 12;                  % Number of external inputs
  Nh = 3;                   % Number of hidden units
  No = 1;                   % Number of output units (classes -1)
  randn('seed',0);          % Initialize the random generator
  % Weight initialization
  range=.5;
  
  alpha_i = 1e-2;           % Input weight decay
  alpha_o = 1e-2;           % Output weight decay
  
  iterations = 100; 
  greps = 1e-4;             % Gradient norm stopping criteria
  
  %%%%%%%%%%%%%%%%%%%%
  % Load data        %
  %%%%%%%%%%%%%%%%%%%%
  
  load sp
  x0 = x0(1:12,:)';
  x1 = x1(1:12,:)';
  x2 = x2(1:12,:)';
  y0 = y0';
  y1 = y1';
  y2 = y2';
  

  %%%%%%%%%%%%%%%%%%%%


  
  % Training of the net
  N = 1;
  E = zeros(5,N);
  for n = 1:N
    % Initialize network weights
    seed = sum(100*clock);
    [Wi_init,Wo_init] = nr_winit(Ni, Nh, No, range, seed);

    
    % Gradient
    [Wi, Wo, Etrain] = nr_trainx(Wi_init, Wo_init, alpha_i, alpha_o, ...
	x0, y0, iterations, 0, greps, 1);
    E(1, n) = Etrain(end);
    title('Gradient descent')
    
    % Pseudo-Gauss-Newton
    [Wi, Wo, Etrain] = nr_trainx(Wi_init, Wo_init, alpha_i, alpha_o, ...
	x0, y0, 10, iterations-10, greps, 2);
    E(2, n) = Etrain(end);
    title('Pseudo-Gauss-Newton');
    
    [Wi, Wo, Etrain] = nr_trainx(Wi_init, Wo_init, alpha_i, alpha_o, ...
	x0, y0, 0, iterations, greps, 3, 'FR');
    E(3, n) = Etrain(end);
    title('Conjugate gradient: Fletcher-Reeves');

    [Wi, Wo, Etrain] = nr_trainx(Wi_init, Wo_init, alpha_i, alpha_o, ...
	x0, y0, 0, iterations, greps, 4, 'HS');
    E(4, n) = Etrain(end);
    title('Conjugate gradient: Hestenes-Stiefel')
    
    % Polak-Ribiere
    [Wi, Wo, Etrain] = nr_trainx(Wi_init, Wo_init, alpha_i, alpha_o, ...
	x0, y0, 0, iterations, greps, 5, 'PR');
    E(5, n) = Etrain(end);
    title('Conjugate Gradient: Polak-Ribiere');
    
  end



