Is there a way to plot multiple neural network run results into one plot?

9 views (last 30 days)
Hello,
I am utilizing a shallow neural network to analyze a large dataset. I'm running the data through the network 100 times to get an idea of the best fit. Is there a way to create a plot where the results from all 100 runs are combined into one figure? Currenlty the network produces just one graph for the last run through the network.
Thanks!

Answers (1)

Divya Gaddipati
Divya Gaddipati on 6 Jan 2020
You can use the field OutputFcn of the trainingOptions function.
You can refer to the following example and change it according to your need.
clc; clear; close all;
% Data
[XTrain,YTrain] = digitTrain4DArrayData;
idx = randperm(size(XTrain,4),1000);
XValidation = XTrain(:,:,:,idx);
XTrain(:,:,:,idx) = [];
YValidation = YTrain(idx);
YTrain(idx) = [];
% Network
layers = [
imageInputLayer([28 28 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
max_epoch = 5;
miniBatchSize = 128;
% Number of iteration in an epoch with miniBatchSize as 128
total_iterations = round(length(YTrain)/miniBatchSize);
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',5, ...
'MiniBatchSize',miniBatchSize, ...
'ValidationData',{XValidation,YValidation}, ...
'Plots','training-progress', ...
'OutputFcn',@(info)savetrainingdata(info, total_iterations));
% Train network
net = trainNetwork(XTrain,YTrain,layers,options);
function stop = savetrainingdata(info, total_iterations)
stop = false; %prevents this function from ending trainNetwork prematurely
% Keep track of the training loss and accuracy for each iteration in an epoch
persistent train_loss
persistent train_acc
persistent results
if info.State == "start"
train_loss = [];
train_acc = [];
end
if info.State == "iteration"
train_loss = [train_loss; info.TrainingLoss];
train_acc = [train_acc; info.TrainingAccuracy];
end
% For each epoch, save the training loss and accuracy
if(info.State == "iteration" && info.Iteration == info.Epoch*total_iterations)
all_val = [train_loss, train_acc];
results{info.Epoch} = all_val;
% you can also plot the graph
train_loss = [];
train_acc = [];
end
if info.State == "done" %check if all epochs have completed
save('results.mat', 'results');
end
end
Finally, you can load the results.mat and plot the training loss and accuracy for all the epochs.
For more information on how to use OutputFcn, refer to the link here
Hope this helps!
  2 Comments
Tyler Menz
Tyler Menz on 13 Jan 2020
Thank you!! Is there a way to get this code to save the p-value and r2 the network makes as well?
Divya Gaddipati
Divya Gaddipati on 14 Jan 2020
Yes, you can add that in the fourth "if" loop (i.e., if(info.State == "iteration" && info.Iteration == info.Epoch*total_iterations))

Sign in to comment.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!