Optimization of hidden layer dimension in neural network (number of neurons)
조회 수: 13 (최근 30일)
이전 댓글 표시
I am using GA optimization to train my neural network (to try to find the best weights set):
% Neural Network trained by Genetic Algorithm (GA)
% Reference: http://www.mathworks.com/matlabcentral/answers/100323
% Thx Greg!
rng('default')
[x, t] = simplefit_dataset;
[I, ~] = size(x);
[O, N] = size(t);
% Reference MSE: Average Target Variance
var_t = mean(var(t,1,2));
% Hidden Node Choice
H = 4;
Nw = (I + 1)*H + (H + 1)*O;
% Create Regression/Curve-Fitting Neural Network:
net = feedforwardnet(H);
net.divideFcn = 'dividetrain';
% Configure the Net for the Simplefit Dataset
net = configure(net, x, t);
% Initial Weights and Errors
wb = getwb(net)';
% Create handle to the error function,
fun = @(wb) nmse(wb, net, x, t); % NMSE
% Set the Genetic Algorithm tolerance for minimum change in fitness function
% before terminating algorithm to 1e-4 and display each iteration's results.
opts = optimoptions('ga', 'FunctionTolerance', 1e-4, 'Display', 'iter');
tic
[wbopt, fval] = ga(fun, Nw, opts);
totaltime = toc;
% Assign the weights to net
net = setwb(net, wbopt');
% Simulate the output
y = sim(net,x);
Where the function nmse is:
function nmse_calc = nmse(wb, net, input, target)
% 'wb' contains the weights and biases vector
% in row vector form as passed to it by the
% genetic algorithm. This must be transposed
% when being set as the weights and biases
% vector for the network.
% Reference MSE
var_t = mean(var(target,1,2));
% To set the weights and biases vector to the
% one given as input
net = setwb(net, wb');
% To evaluate the ouputs based on the given
% weights and biases vector
y = net(input);
% Calculating the Normalised Mean Squared Error (NMSE)
nmse_calc = mean((target(:) - y(:)).^2) / var_t;
end
My question is:
Is there any way to optimize the number of hidden nodes H in the same way? Maybe with multiobjective GA optimization... optimizing the weights set and the number of hidden nodes at once.
Thank you very much!
댓글 수: 1
PIYUSHA GARG
2020년 6월 12일
편집: PIYUSHA GARG
2020년 6월 12일
please tell how to write Nw for multiple hidden layers.
채택된 답변
Greg Heath
2017년 12월 20일
편집: Greg Heath
2017년 12월 20일
If you search the NEWSGROUP and ANSWERS using
greg genetic
you should conclude that I don't recommend GA for NN design.
Nevertheless, if you insist, I recommend a double loop design:
j =0
for h = Hmin:dH:Hmax
j=j+1
standard stuff
for i = 1:Ntrials
GA
end
end
Hope this helps,
Thank you for formally accepting my answer
Greg
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!