clc; close all; clear all;
load basisData.mat
[n,d] = size(X);

%%Standardize data
XOld=X;
yOld=y;
minX=min(X);
X=X-min(X);
maxX=max(X);
X=X./max(X);
minY=min(y);
y=y-min(y);
maxY=max(y);
y=y./max(y);

% Prepare the new file.
vidObj = VideoWriter('overfitNeuralNetwork.mp4','MPEG-4');
open(vidObj);

%Chose the type of Neural Net: 'overfit' or 'underfit'
%typeNN='underfit'
typeNN='underfit'

% Choose network structure
if strcmp(typeNN,'overfit')
    nHidden = [65 203 65];
    lambda=ones(length(nHidden)+1,1)*0;
    maxIter = 30000;
else
    nHidden = [27 40 27];
    lambda=ones(length(nHidden)+1,1)*0.001;
    maxIter = 20000;
end

% Count number of parameters and initialize weights 'w'
nParams = d*nHidden(1);
for h = 2:length(nHidden)
    nParams = nParams+nHidden(h-1)*nHidden(h);
end
nParams = nParams+nHidden(end);
w = randn(nParams,1);

% Train with stochastic gradient
stepSize = 5e-4;
funObj = @(w,i)MLPregressionLoss(w,X(i,:),y(i),nHidden,lambda,typeNN);
for t = 1:maxIter
    if t > maxIter/2
        stepSize = stepSize * (1-2/maxIter)^2;
    end
    wOld = w;
    wOldOld= wOld;
    % The actual stochastic gradient algorithm:
    i = ceil(rand*n);
    [f,g0] = funObj(w,i);
    %batch size
    iterN=20;
    g=g0./iterN;
    for iter=1:(iterN-1)
        i = ceil(rand*n);
        [f,gi] = funObj(w,i);
        g=g+gi./iterN;
    end
    w = w - stepSize*g + stepSize^2*(w-wOldOld) ;

    % Every few iterations, plot the data/model:
    if mod(t-1,round(maxIter/127)) == 0
        %fprintf('Training iteration = %d\n',t-1);
        figure(1);clf;hold on
        XhatOld = [minX:.05:(maxX+minX)]';
        Xhat=(XhatOld - minX)./maxX;
        yhat = MLPregressionPredict(w,Xhat,nHidden,typeNN);
        plot(XOld,yOld,'.');
        h=plot(XhatOld,((yhat.*maxY)+minY),'g-','LineWidth',3);
        drawnow;
        writeVideo(vidObj,getframe);
    end

end

close(vidObj);
typeNN =

underfit

Browser Doesn't Support MP4 files.
Using deep learning to interpolate the data whitout overfitting
    typeNN =
    
    overfit
    
Browser Doesn't Support MP4 files.
Using deep learning to interpolate the data with more overfitting