Neural Networks in a loop with a condition
4 views (last 30 days)
Show older comments
Hi everyone.
I am using a code to simulate an input output response using neural networks.
Then I want to calculate the error between the power_sim and power_obs, and I want to keep the neural networks keep simulating untill the error between power_sim and power_obs is less than a value X that already I fixed.
% Input
sheet1 = 1; xlRange1 = 'B2:B288'; ANN_Inputs = xlsread(filename,sheet1,xlRange1);
%Target
sheet1 = 1; xlRange1 = 'C2:C288'; ANN_Target = xlsread(filename,sheet1,xlRange1)';
% The neural networks model
net=feedforwardnet([10 15 5]);
net.divideParam.trainRatio=0.8;
net.divideParam.testRatio=0.1;
net.divideParam.valRatio=0.1;
net.trainParam.lr=0.001;
net.trainParam.min_grad=1e-20;
net.trainParam.goal=1e-3;
net.trainParam.epochs=1000;
net.trainParam.show=20;
net.trainParam.max_fail=1000;
net.trainFcn = 'trainlm';
net.trainParam.mu=0.01;
net=train(net,ANN_Inputs,ANN_Target);
net_output1=net(ANN_Inputs);
Obs=ANN_Target';
Sim=net_output1';
% Here I calculated power spectrum of the target
y1 = fft(ANN_Target');
y1(1) = [];
n1 = length(y1);
power_obs = abs(y1(1:floor(n1/2))).^2;
maxfreq1 = 1/2;
freq1 = (1:n1/2)/(n1/2)*maxfreq1;
period_obs = 1./freq1;
period_obs=period_obs';
% Power spectrum of simulated
y1 = fft(Sim);
y1(1) = [];
n1 = length(y1);
power_sim = abs(y1(1:floor(n1/2))).^2;
maxfreq1 = 1/2;
freq1 = (1:n1/2)/(n1/2)*maxfreq1;
period_sim = 1./freq1;
period_sim=period_sim';
% Then I want to calculate the error between the power_sim and power_obs, and I want to keep the neural networks running again
% untill the error between the two is less than a value X.
2 Comments
John D'Errico
on 15 Oct 2024
Such a threshold need not be a good idea. In fact, it arguably is rarely so.
Suppose your data is too noisy? That is, more noise then the threshold? Now even if you can fit the model and meet the threshold, then you will be overfitting the data, predicting noise. Or you may never be able to meet that threshold.
Suppose the threshold is too high? Now the fit would stop at a point before the model adequately represnets the signl in your data. You will be underfitting the data.
In either case, you are likely making a mistake, unless your choice for that threshold is perfectly on-target.
Answers (1)
Jaimin
on 28 Oct 2024
To accomplish this, you should establish a loop that keeps training your neural network until the discrepancy between "power_sim" and "power_obs" falls below a specified threshold (“X”). Below is a general framework for implementing this in MATLAB with the Deep Learning Toolbox.
% Training loop
while error >= error_threshold
% Train the network
[net, tr] = trainnet(net, input_data, power_obs);
% Simulate the network
power_sim = net(input_data);
% Calculate the mean squared error
error = perform(net, power_obs, power_sim);
% Display the current error
fprintf('Current error: %f\n', error);
end
To train an ANN, you can use the MATLAB “trainnet” function. For more details, please refer to the following MathWorks documentation.
I hope this will be helpful.
0 Comments
See Also
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!