How to initialize weight and biases for single hidden layer ?

12 views (last 30 days)
Hello Everyone ! Please i need your help !
In order to create artificial neural networks for solar radiation prediction, I need to define architecture of my NNs, especially the number of hidden neurons because i use one hidden layer. So, i would like to use the following approach: Fix the initial conditions (weights and biases) and vary the number of hidden neurons from 1 to 20, till i find architecture that gives best performance.
My questions is : How do i use 'net = init(net)' in this code ?
x = simplefitInputs;
t = simplefitTargets;
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. NFTOOL falls back to this in low memory situations.
trainFcn = 'trainlm'; % Levenberg-Marquardt
% Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize,trainFcn);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
% View the Network
view(net)
If there are other approaches, do not hesitate to mention them.
Thank you.

Answers (1)

Purvaja
Purvaja on 7 Feb 2025 at 6:53
According to your question, you wish to try out network with different number of hidden neurons in your single hidden layer neural network to get best possible performance. Also, you want to set the initial weights and biases.
The init” function in MATLAB initialises initial weights and biases to zero. To initialise weights and biases and to evaluate performance with different number of hidden layer neurons, follow the given steps:
% Initialise dataset
% Choose a Training Function
% Define the range of hidden neurons to test
hiddenLayerSizes = 1:20;
% Initialize a variable to store performance results
performanceResults = zeros(length(hiddenLayerSizes), 1);
% Loop over different hidden layer sizes
for i = 1:length(hiddenLayerSizes)
% Create a Fitting Network with the current hidden layer size
hiddenLayerSize = hiddenLayerSizes(i);
net = fitnet(hiddenLayerSize, trainFcn);
% Setup Division of Data for Training, Validation, Testing
% Initialize the network using init(net)
net = init(net);
% Train the Network
% Test the Network
% Store the performance result
performanceResults(i) = performance;
end
% Find the best performing architecture
[bestPerformance, bestIndex] = min(performanceResults);
bestHiddenLayerSize = hiddenLayerSizes(bestIndex);
For more clarification, following documentation links would be helpful:
You can also try this command in your MATLAB command window:
help network/init
Hope this helps you!

Community Treasure Hunt

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

Start Hunting!