clearvars
load dataset.mat

%

y = dummyvar(y)'; % one-hot-encoding

%

net = patternnet([50,20]); 
% first hidden layer = 50
% second hidden layer = 20
net.trainParam.epochs = 100;
net.trainParam.time = 60;
net.divideParam.trainRatio = 0.9;
net.divideParam.valRatio = 0.1;
net.divideParam.testRatio = 0;
view(net)

%

[net,tr] = train(net,X,y, ...
    'useParallel','yes', ...
    'showResources','yes');

%

pred = net(Xt);
[~,preds] = max(pred);
truth = double(yt);

%

accuracy = 1 - nnz(truth - preds)/length(truth)
confusionchart(truth,preds)

%

error_id = find(abs(truth-preds))
k = error_id(400);
imagesc(reshape(Xt(:,k),28,28))
disp(['True: ' num2str(truth(k)-1) ', pred: ' num2str(preds(k)-1) ', index: ' num2str(k)])