
% Online perceptron learning exampe from Sebastian Seung - MIT

load('/Users/Shared/MatLab_Routines/ps2faces.mat')

f = inline('1./(1+exp(-x))');   % definition of sigmoid

[n,m]=size(train);  % number of pixels and number of examples
s=sqrt(n);
eta=0.01;  % learning rate (start =0.1)
epsinit=0.01;  % parameter controlling magnitude of initial conditions
w=epsinit*randn(n,1);   % initialization with Gaussian random values
b=epsinit*randn;

tmax=100000; %start = (10000)
errsq=zeros(tmax,1);   % squared error on training set

for t=1:tmax
  i=ceil(m*rand);
  x=double(train(:,i))/255;   % uint8 -> double, normalize max value to one
  y=double(trainlabels(i));
  actual=f(w'*x+b);
  error=y-actual;
  slope=actual*(1-actual);
  errsq(t)=error'*error;
  delta=slope*error;  % error signal scaled by derivative
  w=w+eta*delta*x;
  b=b+eta*delta;
  if rem(t,100)==0
    subplot(2,1,1)
    imagesc(reshape(w,s,s))
    title(sprintf('w, t=%d',t))
    axis image off
    colormap gray
    subplot(2,1,2)
    semilogy(1:t,cumsum(errsq(1:t))./(1:t)')
    drawnow
  end
end                                  

disp(sprintf('mean squared error: %.4f',mean(errsq)))

 %look at result with test set
for m=1:2000
   x=double(test(:,m))/255;   % uint8 -> double, normalize max value to one
   y=double(testlabels(m));
   predict=f(w'*x+b);
   colormap gray
   imagesc(reshape(test(:,m),s,s))
drawnow
    title(sprintf('test, %d, predict, %7.3f, set, %7.3f',m, predict, y))
 drawnow
 pause (0.2)
end

 %look at result with training set
 %(poor quality images compared to training set)
for m=1:6977
   x=double(train(:,m))/255;   % uint8 -> double, normalize max value to one
   y=double(trainlabels(m));
   predict=f(w'*x+b);
   colormap gray
   imagesc(reshape(train(:,m),s,s))
drawnow
    title(sprintf('train, %d, predict, %7.3f, set, %7.3f',m, predict, y))
 drawnow
 pause (0.2)
end
 
%look at test set
for m=1:2000
    colormap gray
   imagesc(reshape(test(:,m),s,s))
     title(sprintf('test, m=%d, set,%d',m, testlabels(m)))
 drawnow
end

%look at training set
for m=1:6977
    colormap gray
   imagesc(reshape(train(:,m),s,s))
    title(sprintf('train, m=%d, set,%d',m, trainlabels(m)))
  drawnow
end



