function [wh,wo,es] = nntrnviz(dat,nrm,tgt,lr,its,hus)
% [wh,wo,es] = nntrnviz(dat,nrm,tgt,lr,its,hus)
%    Visualize training of neural net by plotting surfaces after 
%    each iteration.
%    It's special-cased for 3 outputs and 50 pts per output.
% 2002-02-11 dpwe@ee.columbia.edu

% First iteration is different to intialize net
[wh,wo,es] = nntrain(dat,nrm,tgt,lr,1,hus);

ptsperop=50;

uu=1:ptsperop;
oo=ptsperop+[1:ptsperop];
aa=(2*ptsperop)+[1:ptsperop];

% Limits for gridsampling
limits = [200 1100 600 1600];
gridpts = 60;
% 3D viewpoint
az = 18;
el = 44;

for e = 1:its
	
    plot(dat(uu,1),dat(uu,2),'or',dat(oo,1),dat(oo,2),'ob',dat(aa,1),dat(aa,2),'og');
    % Sample entire 2D surface of NN outputs
    [nnO,xx,yy] = nngridsamp(wh,wo,nrm,limits,gridpts,1);
    [nnU,xx,yy] = nngridsamp(wh,wo,nrm,limits,gridpts,2);
    [nnA,xx,yy] = nngridsamp(wh,wo,nrm,limits,gridpts,3);
    % Use contour to plot where their difference crosses zero
    hold on
    contour(xx,yy,nnO-max(nnU,nnA),[0 0])
    contour(xx,yy,nnA-max(nnU,nnO),[0 0])
    % Pretty good decision boundary.  You can see the 'wrong' points
    % Add the actual surfaces defined by the outputs
    % (shifted down by -1 so 2D plane is at the top)
    surf(xx,yy,nnO-1)
    surf(xx,yy,nnU-1)
    surf(xx,yy,nnA-1)
    view(az,el);
    hold off
    pause;
    
    % Do the next iteration
    [wh,wo,esb] = nntrain(dat,nrm,tgt,lr,1,wh,wo);
    es = [es,esb];

end
