Clear Filters
Clear Filters

problem with rng during retraining of a neural network

1 view (last 30 days)
Hello
I have a trained a neural network on a data set and tested its generalization on an independent sample. I have deliberately reduced the training equations and increased the number of hidden nodes to make the net overfit since I am going to use some methods to avoid overfitting later. The problem is with rng. I have saved the rng state but when I use the same state for traing my net again, I get different results! here is the code:
[x,t]=bodyfat_dataset;
Q = size(x,2);
Q1 = floor(Q*0.30);
Q2 = Q-Q1;
Q3=floor(Q2*0.15);
ind = randperm(Q1+Q3);
ind1 = ind(1:Q1);
ind2 = ind(Q1+(1:Q3));
x1 = x(:,ind1);
t1 = t(:,ind1);
x2 = x(:,ind2);
t2 = t(:,ind2);
[I Ntrn] = size (x1);
[O Ntrn] = size (t1);
Ntrneq = prod(size(t1));
MSEtrn00 = mean(var(t1',1)) ;
Hub = -1 + ceil( (Ntrneq-O) / (I+O+1))
MSEgoal = 0.01*MSEtrn00;
MinGrad = MSEgoal/10;
Hmax = 10;
dH=1
Hmin =0
Ntrials = 20
rng(0)
j=0
for h = Hmin:dH:Hmax
j=j+1
if h==0
net = newff(x1,t1,[]);
Nw = (I+1)*O
else
net = newff(x1,t1,h);
Nw = (I+1)*h+(h+1)*O
end
Ndof = Ntrneq-Nw;
net.divideFcn = 'dividetrain';
net.trainParam.goal = MSEgoal;
net.trainParam.min_grad = MinGrad;
for i = 1:Ntrials
h = h
ntrial = i
net = configure(net,x1,t1);
[ net tr Ytrn ] = train(net,x1,t1);
ytrn = round(Ytrn)
MSEtrn = mse(t1-ytrn);
R2trn= 1-MSEtrn/MSEtrn00;
Ytst = net(x2)
MSEtst(i,j)=mse(net,t2,Ytst);
s(i,j) = rng;
end
end
.....................................
suppose that the best result was obtained for s(3,2) then for retraining of my net:
rng(s(3,2))
net=newff(x1,t1,2);
net.divideFcn='dividetrain';
net=train(net,x1,t1);
y2=net(x2);
perf=mse(net,t2,y2)
% the perf is diffrent than that of s(3,2) during the previous training!!!

Accepted Answer

Greg Heath
Greg Heath on 19 Dec 2013
randperm is used without initializing the RNG.
Thank you for formally accepting my answer
Greg
  2 Comments
Mohamad
Mohamad on 19 Dec 2013
Dear Greg Thank you very much. The rng was initialized but the problem with this code was not solved!! best
Greg Heath
Greg Heath on 19 Dec 2013
I do not understand your reply:
Randperm was called before rng(0). Therefore, if you run the code multiple times you will get multiple answers.

Sign in to comment.

More Answers (0)

Categories

Find more on Sequence and Numeric Feature 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!