If I want to make a prediction from a neural net, do I have to use the same random number generator seed?

4 views (last 30 days)
I have a question about the state of the random number generator when I make a prediction with a neural network. Let's say I loop over different values of the number of neurons H to include in my net, and find that 5 is optimal. At the end of the loop, the random number generator happens to be in a different state than when the H=5 net was trained. I then want to use the H=5 net to make predictions using some new input data. Do I first have to set the random number generator to its state when the H=5 net was trained (example below)?
Similarly if I save the net to a mat file, quit Matlab, and restart it, can I leave rng at its default state, load the saved H=5 net, and use it to make a prediction?
[x,t] = simplefit_dataset; % Load example data to fit
[nVar, nExamples] = size(x);
rng(0); % Set the random number generator
hList = 1:2:10; % Candidate number of neurons to loop through
for iNeuron = 1:length(hList) % Loop through candidate # of neurons
All.rng{iNeuron} = rng; % Store the state of the rng, which changes
h = hList(iNeuron); % Number of neurons
net = fitnet(h,'trainbr'); % Fit the network
net = train(net,x,t); % Train the network
All.net{iNeuron} = net; % Store the network
end
% After exiting this loop, rng is in a different state than for the H=5 case
% Based on some optimization (not shown), let's say 5 is best number of neurons
iNeuronDesired = hList == 5;
netBest = All.net{iNeuronDesired}; % Retrieve net that uses optimum # of neurons
xNew = rand(nVar, 100)*10; % Make some new data to base a prediction on
% But first do I have to set the random number generator
% to the value it was at for H=5? For example,
% rng(All.rng{iNeuronDesired});
y = netBest(xNew); % Apply the best net to make a prediction from new data

Accepted Answer

Greg Heath
Greg Heath on 21 Oct 2018
Once a net is trained, the state of the rng is irrelevant since random numbers have no part in typical output approximations.
Hope this helps.
Thank you for formally accepting my answer
Greg

More Answers (0)

Community Treasure Hunt

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

Start Hunting!