Implementing Random Seed for Machine Learning
15 views (last 30 days)
Show older comments
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 Comments
Mohammad Sami
on 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.
Answers (0)
See Also
Categories
Find more on Image 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!