How can I train ANFIS using Particle Swarm Optimization algorithm ?
17 次查看(过去 30 天)
显示 更早的评论
I have my ANFIS developed from a dataset, I want to optimize it using PSO algorithm, please I need someone that will help me through. Please I need guidance, codes or proposed tutorials that I will easily learn how to optimize the ANFIS with PSO.
2 个评论
Sam Chak
2023-9-27
Hi @Ahmad
If I remember correctly, the Adaptive Network component of ANFIS has the capability to minimize the prescribed error measure using the hybrid learning method: Gradient Descent + Least Square Estimation. What role does the PSO play when you mention 'optimizing the ANFIS with PSO'? Are you suggesting replacing the optimizing 'AN' part with 'PSO', effectively creating 'PSOFIS'?
采纳的回答
Sam Chak
2023-10-11
Hi @Ahmad
The output (target) of the patient data constitutes a Boolean function, yielding values from a two-element set {0, 1}. It generates discrete outputs contingent on various input value combinations. Consequently, without postprocessing, I have reservations about ANFIS's ability to provide predictions based on input data, as fuzzy systems yield intermediate truth-values between true (1) and false (0). One viable approach is to round the predicted floating-point output values of the FIS.
For your information, I refrained from splitting the observational data into training and cross-validation sets. This decision was influenced by the belief that a continuous function might not aptly capture the Boolean function, unless one considers the Heaviside step function. Consequently, there exists a certain percentage of predicted targets that significantly deviate from the observational targets, which I have denoted as "outliers" (not to be confused with the statistical > 3σ outliers). One plausible explanation for these outliers may be attributed to unobserved parameters, such as Age, Sex, Family history, Smoking, Obesity, Sedentary lifestyle, and Stress.
And nope, the Particle Swarm Optimization (PSO) algorithm won't help for the same reasons explained above.
% Observational Data
data = readtable('hearts.csv');
X = table2array(data(:, 1:6)); % 6 Inputs of patient data
Y = table2array(data(:, 7)); % 1 Output (target)
data_Train = [X Y];
% Setting up the initial FIS using Subtractive Clustering method
genOpt = genfisOptions('SubtractiveClustering');
inFIS = genfis(X, Y, genOpt);
anOpt = anfisOptions('InitialFIS', inFIS, 'EpochNumber', 500);
% Training data with ANFIS
outFIS = anfis(data_Train, anOpt)
% Predict using trained FIS & display RMSE
Ypred1 = round(evalfis(outFIS, X));
rmse1 = sqrt(mean((Y - Ypred1).^2));
txt1 = sprintf('RMSE of the prediction is %.4f.', rmse1);
disp(txt1)
% Identify outliers by finding indices
Outliers = abs(Y - Ypred1); % Forecasts that differ significantly from observations
idx = find(Outliers == 1);
numOutliers = length(idx);
numSamples = length(Y);
Percentage = 100*numOutliers/numSamples;
txt2 = sprintf('%2.2f percent of the %d samples are outliers, which is equal to %d.', Percentage, numSamples, numOutliers);
disp(txt2)
% Get Prediction 2
Ypred2 = Ypred1;
Ypred2(idx) = ~Ypred2(idx);
rmse2 = sqrt(mean((Y - Ypred2).^2));
txt3 = sprintf('RMSE of the prediction with outlier factor is %.4f.', rmse2);
disp(txt3)
3 个评论
Sam Chak
2023-10-11
Hi @Ahmad
You are welcome, @Ahmad. However, I'm not a medical doctor and I believe I have already exhausted my mental resources today in attempting to solve the original data training problem as posted. If you find the guided solution and explanation about ANFIS helpful, please consider clicking 'Accept' ✔ on the answer and voting 👍 for it. Thanks a bunch!
As for the new data training problem, which involves significantly higher dimensions with 13 inputs, could you attempt to work through it using the guidance provided earlier? Please share the results or any error messages if encountered.
更多回答(2 个)
Sam Chak
2023-9-27
Hi @Ahmad
To achieve a lower prescribed error measure, you can manually increase the number of Membership Functions until the desired error measure is reached. Naturally, I believe that PSO (Particle Swarm Optimization) can be used to find the optimal value for this parameter. However, since this is a relatively simple problem, manual tuning or tuning through iteration can also produce the desired result quickly, as demonstrated in the following example.
x = (0:0.1:10)';
y = sin(2*x)./exp(x/5);
% settings for FIS 1
genOpt1 = genfisOptions('GridPartition');
genOpt1.NumMembershipFunctions = 5; % <-- initial number of MFs
genOpt1.InputMembershipFunctionType = 'gaussmf';
inFIS1 = genfis(x, y, genOpt1);
opt = anfisOptions('InitialFIS', inFIS1);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;
% compute RMSE of FIS 1
outFIS1 = anfis([x y], opt);
yFIS1 = evalfis(outFIS1, x);
Error1 = rmse(yFIS1, y)
% settings for FIS 2
genOpt2 = genfisOptions('GridPartition');
genOpt2.NumMembershipFunctions = 25; % <-- number of MFs is increased 5x
genOpt2.InputMembershipFunctionType = 'gaussmf';
inFIS2 = genfis(x, y, genOpt2);
opt = anfisOptions('InitialFIS', inFIS2);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;
% compute RMSE of FIS 2
outFIS2 = anfis([x y], opt);
yFIS2 = evalfis(outFIS2, x);
Error2 = rmse(yFIS2, y)
% plots
plot(x, abs(y - yFIS1), x, abs(y - yFIS2)), grid on
xlabel('x')
ylabel('Absolute Error')
legend('FIS 1', 'FIS 2')
7 个评论
Sam Chak
2023-10-10
I commented out setMembershipFunctionsCustom(), and it works on my data. Honestly, I don't know how your user-supplied PSO code works. The 'anfis()' function itself has a built-in hybrid optimization algorithm, 'Gradient Descent + Least Square Estimation'. If you want me to investigate the data training issue using anfis, please attach the data 'hearts.csv', provided that the patient data is anonymized.
Sam Chak
2023-10-12
Hi @Ahmad
For the new data "heart_dataset.csv" with 13 inputs to 1 output, the Subtractive Clustering method will generate 209 rules because each fuzzy input has 209 membership functions. While the number of rules may seem daunting, if the Grid Partitioning method is used with the fewest number of MFs for each input, even more rules will be required—specifically, (2 MFs)^(13 Inputs) = 8192 rules. Nonetheless, with my proposed approach, the RMSE for predicting the 13-input Boolean function is ZERO (0). You can express the ANFIS-based Boolean function as follows:
where the firing strength of a rule k is given by
,
and the first-order conclusion is a linear function given by
,
with to are all constants.
% Observational Data
data = readtable('heart_dataset.csv');
X = table2array(data(:, 1:13)); % 13 Inputs of patient data
Y = table2array(data(:, 14)); % 1 Output (target)
data_Train = [X Y];
% Setting up the initial FIS using Subtractive Clustering method
genOpt = genfisOptions('SubtractiveClustering');
inFIS = genfis(X, Y, genOpt);
anOpt = anfisOptions('InitialFIS', inFIS, 'EpochNumber', 3);
% Training data with ANFIS
outFIS = anfis(data_Train, anOpt)
% Predict using trained FIS & display RMSE
Ypred1 = round(evalfis(outFIS, X));
rmse1 = sqrt(mean((Y - Ypred1).^2));
txt1 = sprintf('RMSE of the prediction is %.4f.', rmse1);
disp(txt1)
3 个评论
Sam Chak
2023-10-14
Hi @Ahmad
I'm feeling a bit confused about the approach that has been taken. The code is roughly 80% identical to the solution, except for the fact that the data is divided in a biased manner into training and cross-validation sets. Additionally, the initial FIS is set up in an unguided manner using the Grid Partitioning method with the user-specified number of Membership Functions (MFs) as [4 3 2 2 4 3] for the six fuzzy inputs.
Since the problem of training the full dataset using ANFIS has been satisfactorily resolved, and you are continuing to pursue the training solution for partial data using PSO, I encourage you to create a New Question and specify the specific problem you are facing. If your PSO works flawlessly for other test problems like the Rosenbrock Function, it is likely not a coding issue but a mathematical formulation problem.
Please ensure that the title of your question is clear, for example: "How can I formulate the optimization problem to train a user-specified model using PSO?" The model can be ANFIS, Convolutional Neural Networks (CNN), or Support Vector Machines (SVM).
Things to consider:
It is still not clear which parameters in the internally-optimized ANFIS are intended to be optimized using PSO. There are many parameters to consider, such as the partitioning technique, number of MFs, input and output MF types, as well as other options specific to the partitioning techniques. Please identify these parameters.
If the mathematical modeling of the patients' heart conditions is unimportant to you, and you don't need to explain the nature or mechanism of the model set, then consider training the data using Neural Networks, where you can employ binary classifiers.
Please be reminded to share your user-developed PSO code for optimization purposes and the MATLAB-built-in ANFIS code for training purposes in the New Question.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fuzzy Inference System Tuning 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!