Implementing Random Seed for Machine Learning

19 Ansichten (letzte 30 Tage)
Sofia Brown
Sofia Brown am 14 Sep. 2021
Kommentiert: Sofia Brown am 16 Sep. 2021
I am building a convolutional network and it was suggested to me to set the same random seed at the beginning of my code using the rng command in order to achieve reproducibility of the training results. Here is my neural network:
rootFolder = 'TrainingAll5Sets';
categories = {'0deg', 'eighthdeg'};
rng(0);
%imds = imageDatastore(fullfile(rootFolder, categories), 'LabelSource', 'foldernames');
imds = imageDatastore(fullfile(rootFolder, categories), 'LabelSource', 'foldernames','FileExtensions','.png');
%Define Layers
layers = [
imageInputLayer([256 320 1])
convolution2dLayer(1,5,'Padding',2)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(6,15,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(12,40,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
%Set training options - use default options from 7.15.20
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.00001, ...
'MaxEpochs',300, ...
'Shuffle','every-epoch', ...
'Verbose',false, ...
'Plots','training-progress');
%Train
[net, info] = trainNetwork(imds, layers, options);
My question is have I implemented the rng command correctly in line 3? My understanding is that rng will generate the same random weights so that I will get reproducible results. Is inserting rng in the beginning of my code sufficient to do this? I want to understand how rng works in a machine learning algorithm.
  2 Kommentare
Mohammad Sami
Mohammad Sami am 15 Sep. 2021
This should be sufficient. You can try it yourself by running your code more then once to verify that you get the same results.
Sofia Brown
Sofia Brown am 16 Sep. 2021
Yes, that worked, thank you.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Image Data Workflows finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by