Implementing Random Seed for Machine Learning

19 ビュー (過去 30 日間)
Sofia Brown
Sofia Brown 2021 年 9 月 14 日
コメント済み: Sofia Brown 2021 年 9 月 16 日
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.
Sofia Brown
Sofia Brown 2021 年 9 月 16 日
Yes, that worked, thank you.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by