Implementing Random Seed for Machine Learning

7 次查看(过去 30 天)
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 个评论
Mohammad Sami
Mohammad Sami 2021-9-15
This should be sufficient. You can try it yourself by running your code more then once to verify that you get the same results.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by