% Multi-layer perceptron

close all

% set up training inputs and outputs for model
%if 1
%   inputs = [1 0; 0 1; 0 0; 1 1];
%   targets = [1 1 0 0]';
%else
%   load datalab1;
%   inputs = [inputsR; inputsB];
%   inputs = inputs./(ones(length(inputs(:,1)),1)*max(inputs));
%   targets = [ones(length(inputsR(:,1)),1); zeros(length(inputsB(:,1)),1)];
%end

newrun = 1;
if newrun
	if 1
		[inputs, targets] = getdata;
	else 
		inputs = rand(50,1);%[0:0.01:1];
		targets = 0.5+sin(inputs*2*pi)/2+0.2*randn(size(inputs));
	end

	% dimensions of training data
	[N,diminputs]  = size(inputs);
	[N,dimoutputs] = size(targets);
	
	
	% initialise the weight vector (add one to include the bias term
	% w0 as first entry of input
	ninput = diminputs + 1;
	nhidden = 5; 
	noutput = 1;
	w1 = 0.1*randn(ninput,nhidden);
	w2 = 0.1*randn(nhidden+1,noutput);  % add one for a bias term (first entry)
end


% normalise the inputs
%inputs  = inputs - ones(N,1)*mean(inputs);
%inputs  = inputs./(ones(N,1)*std(inputs));

eta = 0.1;    % learning rate
maxiter = 10000; % maximum number of training iterations
maxiter = (ceil(maxiter/N));  % scale down maxiters by size of training set
if newrun == 0
	batch = length(errorhistory);
    errorhistory = [errorhistory; zeros(maxiter,1)];
else
	batch = 0;
	errorhistory = zeros(maxiter,1);
end
fprintf('Start learning\n');

for i = 1:maxiter
	batch = batch + 1;
	for t = 1:N
		[outputs, hidden] = mlpforward(inputs(t,:), w1, w2);
		[deltaout, deltahidden] = mlpbackward(hidden, outputs, targets(t,:), w2);
		
		changew2 = [1 hidden]'*deltaout;
		changew1 = [1 inputs(t,:)]'*deltahidden;
		
		w2 = w2 - eta*changew2;
		w1 = w1 - eta*changew1;
	end
	[outputs, hidden] = mlpforward(inputs, w1, w2);
	errorsum = sum((outputs - targets).^2);
	%fprintf('Batch %d, Error = %f\n', batch, errorsum);
	errorhistory(batch) = errorsum;
end


% visualise results
figure(1)
plot(errorhistory);

figure(2)
xrange = [0:0.05:1];       	% grid resolution
mlpresponse(xrange, w1, w2);	% plot results
figure(2)
hold on
if diminputs == 2
plot3(inputs(:,1),inputs(:,2),targets,'o');
elseif diminputs == 1
	plot(inputs,targets,'o')
end
hold off
