%class demo of gradient descent perceptron learning for two input, one
%output perception

%input data ...
x=[[1 1 1 1 1 1 1 0 0 0]' [1 1 1 0 0 0 0 1 1 1]' ];

%training data...
t=[0 0 0 .5 .5 .5 .5 0 0 0]';

L=200;                  %max # of iterations a.k.a. "training epochs"

eta=.01;                %learning rate
alpha=.01;              %weight decay

w=zeros(size(x(1,:)));  %initialize weights to zero (or random), one for each input column

figure;                 %create a figure window


%the training loop.  Here we're going to keep incrementally changing the
%weights to improve the fit to the training data.  Notice there's no early
%stop check here like we had in the gradient descent code.  We just loop a
%specified number of times.

for l=1:L,              %loop L times
    a = x*w';             %compute activations
    y = sigmf(a,[50 0]);  %compute outputs

    e = t-y;              %compute errors

    g = -x'*e;            %compute gradient vector
    w = w-eta*(g'+alpha*w);%make step, using learning
                           %rate eta and weight decay alpha

    %save some variables for later (not part of calculation)
    e_save(l,:) = e; 
    y_save(l,:) = y;
    w_save(l,:) = w;
    pause(.02);

    %animate the changes...
    subplot(2,1,1), plot(e); 
        axis([1 7 -.1 .1]); 
        title('error')
    subplot(2,1,2), plot(y); hold on; plot(t,'r');
        title('training data vs. neuron output')

end





% 
% %=====some plotting (optional)=======
% figure;
% subplot(2,1,1), plot(e);
% title('error');
% subplot(2,1,2), clf; hold off; plot(y); hold on; plot(t,'r');
% title('training data vs. neuron output')
% legend('neuron output','training data')
