Is there a way to plot multiple neural network run results into one plot?
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Divya Gaddipati
2020-1-6
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.
Hope this helps!
2 个评论
Divya Gaddipati
2020-1-14
Yes, you can add that in the fourth "if" loop (i.e., if(info.State == "iteration" && info.Iteration == info.Epoch*total_iterations))
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!