rng seed and cvpartition

2 次查看(过去 30 天)
micholeodon
micholeodon 2020-8-14
Dear Matlab Users,
Why cvpartition keeps choosing same test indices in folds from data, despite rng shuffle (thus different seed values assured) ?
When you run code below twice, you got same value of testInd. Why?
Thank you in advance for you help !
clear;
rng shuffle
parpool(parcluster('local'), 24)
tmp = rng;
seed = tmp.Seed;
nData = 89;
nFolds = 10;
selectedFold = 1;
spmd(3)
tmp = cvpartition(nData, 'KFold', nFold)
x = find(tmp.test(selectedFold)); % extract test indices of fold 1
end
for ii = 1:3
testInd(ii,:) = x{ii};
end
% clear context and
clear x tmp
delete(gcp('nocreate'))

回答(1 个)

Steven Lord
Steven Lord 2020-8-14
You're calling rng shuffle on the client, not on the workers. See this documentation page.
  1 个评论
micholeodon
micholeodon 2020-8-15
Thank you. I can read that:
"By default, the random numbers generated on each worker in a parfor loop are different from each other and from the random numbers generated on the client."
and
"each worker produces a unique sequence of random numbers.".
I have checked that and it is endeed true.
So in the context of my problem, it seems that each time I open MATLAB and run the code same set of random streams is produced and distributed to the workers. How to overcome that i.e. make sure that each time I open matlab and run that code I would get different set of random streams and thus different results? The article you pointed is mostly how to make things reproducible, same.
In RandStream documentation there is Example 7 - maybe this approach is recommended to my problem?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Speed Up Statistical Computations 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by