MATLAB Answers

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

3 ビュー (過去 30 日間)
Tyler Menz
Tyler Menz 2020 年 1 月 3 日
コメント済み: Divya Gaddipati 2020 年 1 月 14 日
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 件のコメント

サインイン to comment.

回答 (1 件)

Divya Gaddipati
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.
For more information on how to use OutputFcn, refer to the link here
Hope this helps!

  2 件のコメント

Tyler Menz
Tyler Menz 2020 年 1 月 13 日
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 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))

サインイン to comment.

サインイン してこの質問に回答します。


Translated by