function [tr]=KI_neural(nH)

alpha=-0.7;
n=500;
K=40;
%nH=floor(n/K);
%nH=2;

MAX_est=12;
MIN_est=-12;




%load tricky
%load moln

[features,targets]=GenerateDataSets;

targets(find(targets))=alpha;
targets(find(targets==0))=-alpha;

Ptrain=features(:,1:n);
Pdata=features(:,n+1:end);

m=length(Pdata);

Validate.P=Pdata(:,1:m/2);
Test.P=Pdata(:,m/2+1:end);

Ttrain=targets(1:n);
Tdata=targets(n+1:end);
Validate.T=Tdata(:,1:m/2);
Test.T=Tdata(:,m/2+1:end);

net=newff([MIN_est MAX_est; MIN_est MAX_est],[nH 1],{'tansig' 'tansig'});


net.trainParam.epochs=600;

size(Ptrain)
size(Ttrain)

%[net,tr]=train(net,Ptrain,Ttrain,[],[],Validate,Test);

[net,tr]=train(net,Ptrain,Ttrain,[],[],[],Test);

% 
% for i=1:10
%     
%     [net,tr]=train(net,Ptrain,Ttrain,[],[],[],Test);
%     
%     figure(10)
%     PlotANNsurface(net,Ptrain)
%     pause
%     figure(1)
% end
% clf


return

sum(isnan(tr.vperf))==length(tr.vperf)

h=semilogy(tr.epoch,tr.perf,'linewidth',2);
hold on
h=[h semilogy(tr.epoch,tr.tperf,'r:','linewidth',2)];


if ~sum(isnan(tr.vperf))==length(tr.vperf)
    h=[h semilogy(tr.epoch,tr.vperf,'g-.','linewidth',2)];
    legend(h,'Training error','Generalization error','Validation error')
else
    legend(h,'Error 1','Error 2')
end
hold off
xlabel('epochs')
ylabel('MSE')

%return

% decisionboundary('sim', Ptrain, Ttrain>0, [net, Test.p], 50)
% pause
Tsim=sim(net,Test.P);

figure(10)
PlotANNsurface(net,Ptrain)





hold on
plot(Test.P(1,1),Test.P(2,1),'.')
CORR=0;

for L=1:length(Test.T)
    if Test.T(L)>0
        str='+';
    else
        str='o';
    end
    if(Tsim(L)*Test.T(L)>0)
        CORR=CORR+1;
        str=['b' str];
    else
        str=['r' str];
    end
    plot(Test.P(1,L), Test.P(2,L),str)    
end


hold off 
ERROR=(length(Test.P)-CORR)/length(Test.P);

title(sprintf('n_H:%d, Error rate %3.3g , MSE: %3.3g',nH,ERROR,tr.perf(end)))



function [features,targets]=GenerateDataSets
NUM=1000;

S1=[1 0.75;1.75 1];
mu1=[1 1];

S2=[1 -0.5;-0.5 1];
mu2=[1 -1];

Y1=[mvnrnd(mu1,S1,NUM) ones(NUM,1)];

Y2=[mvnrnd(mu2,S2,NUM) zeros(NUM,1)];

features=[Y1(:,1:2)' Y2(:,1:2)'];
targets=[Y1(:,3)' Y2(:,3)'];

dummy=rand(size(targets));
[dummy,order]=sort(dummy);


targets=targets(order);
features=features(:,order);


size(targets)