function [net,err,maxdelta]=epoch(net, x, y, lr, eta, beta, method)
%EPOCH	Train the net for one epoch. Internal used.
%
%	[net,err,maxd] = EPOCH (net, x, y, learning_rate, eta, method);
%
%	Inputs:
%	  x, y: input/output data
%	  method: 'seq' and 'bat' 
%	Output: 
%	  net: 1 epoch trained net
%	  err: average training error
%	  maxd: maximum delta
%
%	See also: NEWNET, INITNET, FORWARD, BACKWARD, TRAINNET.

%	Ling Li, Caltech
%	10/21/96, 10/09/00

samples = size(x, 1);
maxdelta = 0;

if method == 'seq' 
	err = 0;
	for i = 1:samples
		[yd, ix] = forward(net, x(i, :));
		delta = backward(net, ix, y(i, :));
		err = err + sum((yd - y(i, :)).^2);

		for l = 1:net.layers-1
%			net.weight{l} = net.weight{l} - lr * ...
%				[-1, ix{l}]' * delta{l};
			deltaw = lr * [-1, ix{l}]' * delta{l};
			maxdelta = max(max(max(abs(deltaw))), maxdelta);
			kbeta = beta{l}^2 ./ ( (beta{l}^2 + net.weight{l}.^2).^2 );
			net.weight{l} = (1-2*lr*eta*kbeta) .* net.weight{l} - deltaw;
		end
	end
else % batch learning
	[yd, ix] = forward(net, x);
	delta = backward(net, ix, y);
	err = sum(sum((yd - y) .^ 2));

	for l = 1:net.layers-1
%		net.weight{l} = net.weight{l} - lr / samples * ...
%			[-ones(samples, 1), ix{l}]' * delta{l};
		deltaw = lr / samples * [-ones(samples, 1), ix{l}]' * delta{l};
		maxdelta = max(max(max(abs(deltaw))), maxdelta);
		net.weight{l} = (1-2*lr*eta)*net.weight{l} - deltaw;
	end
end
err = err / samples;
