Main Content

anfis

Tune Sugeno-type fuzzy inference system using training data

Description

fis = anfis(trainingData) generates a single-output Sugeno fuzzy inference system (FIS) and tunes the system parameters using the specified input/output training data. The FIS object is automatically generated using grid partitioning.

The training algorithm uses a combination of the least-squares and backpropagation gradient descent methods to model the training data set.

example

fis = anfis(trainingData,options) tunes a FIS using the specified training data and options. Using this syntax, you can specify:

  • An initial FIS object to tune.

  • Validation data for preventing overfitting to training data.

  • Training algorithm options.

  • Whether to display training progress information.

example

[fis,trainError] = anfis(___) returns the root mean square training error for each training epoch.

example

[fis,trainError,stepSize] = anfis(___) returns the training step size at each training epoch.

example

[fis,trainError,stepSize,chkFIS,chkError] = anfis(trainingData,options) returns the validation data error for each training epoch, chkError, and the tuned FIS object for which the validation error is minimum, chkFIS. To use this syntax, you must specify validation data using options.ValidationData.

example

Examples

collapse all

Load training data. This data has a single input and a single output.

load fuzex1trnData.dat

Generate and train a fuzzy inference system. By default, the FIS structure is created using a grid partition of the input variable range with two membership functions.

fis = anfis(fuzex1trnData);
ANFIS info:
	Number of nodes: 12
	Number of linear parameters: 4
	Number of nonlinear parameters: 6
	Total number of parameters: 10
	Number of training data pairs: 25
	Number of checking data pairs: 0
	Number of fuzzy rules: 2


Start training ANFIS ...

1 	 0.229709
2 	 0.22896
3 	 0.228265
4 	 0.227624
Step size increases to 0.011000 after epoch 5.
5 	 0.227036
6 	 0.2265
7 	 0.225968
8 	 0.225488
Step size increases to 0.012100 after epoch 9.
9 	 0.225052
10 	 0.22465

Designated epoch number reached. ANFIS training completed at epoch 10.

Minimal training RMSE = 0.22465

Plot the ANFIS output and training data.

x = fuzex1trnData(:,1);
anfisOutput = evalfis(fis,x);
plot(x,fuzex1trnData(:,2),'*r',x,anfisOutput,'.b')
legend('Training Data','ANFIS Output','Location','NorthWest')

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Training Data, ANFIS Output.

The ANFIS data does not match the training data well. To improve the match:

  • Increase the number of membership functions in the FIS structure to 4. Doing so adds fuzzy rules and tunable parameters to the system.

  • Increase the number of training epochs.

opt = anfisOptions('InitialFIS',4,'EpochNumber',40);

Suppress the error and step size Command Window display.

opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;

Train the FIS.

fis = anfis(fuzex1trnData,opt);
ANFIS info:
	Number of nodes: 20
	Number of linear parameters: 8
	Number of nonlinear parameters: 12
	Total number of parameters: 20
	Number of training data pairs: 25
	Number of checking data pairs: 0
	Number of fuzzy rules: 4

Minimal training RMSE = 0.0833853

Plot the ANFIS output and training data.

figure
anfisOutput = evalfis(fis,x);
plot(x,fuzex1trnData(:,2),'*r',x,anfisOutput,'.b')
legend('Training Data','ANFIS Output','Location','NorthWest')

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Training Data, ANFIS Output.

The match between the training data and ANFIS output has improved.

Create single-input, single-output training data.

x = (0:0.1:10)';
y = sin(2*x)./exp(x/5);

Define an initial FIS structure with five Gaussian input membership functions.

genOpt = genfisOptions('GridPartition');
genOpt.NumMembershipFunctions = 5;
genOpt.InputMembershipFunctionType = 'gaussmf';
inFIS = genfis(x,y,genOpt);

Configure the ANFIS training options. Set the initial FIS, and suppress the training progress display.

opt = anfisOptions('InitialFIS',inFIS);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;

Train the FIS using the specified options.

outFIS = anfis([x y],opt);

Compare the ANFIS output with the training data.

plot(x,y,x,evalfis(outFIS,x))
legend('Training Data','ANFIS Output')

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Training Data, ANFIS Output.

Load training and validation data. This data has a single input and a single output.

load fuzex2trnData.dat
load fuzex2chkData.dat

Specify the training options.

opt = anfisOptions(InitialFIS=4,EpochNumber=40);
opt.ValidationData = fuzex2chkData;
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;

Train the FIS, and return the training error.

[trainFIS,trainFISError,~,validationFIS,validationFISError] = ...
    anfis(fuzex2trnData,opt);

trainFISError contains the root mean squared error for the training data at each training epoch. The training error for trainFIS is the minimum value in trainFISError.

trainFISRMSE = min(trainFISError)
trainFISRMSE = 
0.2572

validationFISError contains the root mean squared error for the validation data at each training epoch. The validation error for validationFIS is the minimum value in validationFISError.

validationFISRMSE = min(validationFISError)
validationFISRMSE = 
0.6300

Create single-input, single-output training data.

x = (0:0.1:10)';
y = sin(2*x)./exp(x/5);

Configure the ANFIS training options. Set the initial FIS, and suppress the training progress display.

opt = anfisOptions('InitialFIS',4,'EpochNumber',60);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;

A larger step size increase rate can make the training converge faster. However, increasing the step size increase rate too much can lead to poor convergence. For this example, try doubling the step size increase rate.

opt.StepSizeIncreaseRate = 2*opt.StepSizeIncreaseRate;

Train the FIS, and return the step size array.

[fis,~,stepSize] = anfis([x y],opt);

Plot the step size profile. An optimal step size profile should increase initially, reach a maximum, and then decrease for the rest of the training.

figure
plot(stepSize)

Figure contains an axes object. The axes object contains an object of type line.

Load training data.

load fuzex1trnData.dat

Load validation data.

load fuzex1chkData.dat

Specify the following training options:

  • 4 input membership functions

  • 30 training epochs

  • Suppress training progress display

opt = anfisOptions('InitialFIS',4,'EpochNumber',30);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;

Add the validation data to the training options.

opt.ValidationData = fuzex1chkData;

Train the FIS, and return the validation results.

[fis,trainError,stepSize,chkFIS,chkError] = anfis(fuzex1trnData,opt);

The training error, trainError, and validation error, chkError, arrays each contain one error value per training epoch. Plot the training error and the validation error.

x = [1:30];
plot(x,trainError,'.b',x,chkError,'*r')

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

The minimum validation error occurs at epoch 17. The increase in validation error after this point indicates overfitting of the model parameters to the training data. Therefore, the tuned FIS at epoch 17, chkFIS, exhibits the best generalization performance.

Input Arguments

collapse all

Training data, specified as an array. For a fuzzy system with N inputs, specify trainingData as an array with N+1 columns. The first N columns contain input data, and the final column contains output data. Each row of trainingData contains one data point.

Generally, training data should fully represent the features of the data the FIS is intended to model.

Training options, specified as an anfisOptions option set. Using options, you can specify:

  • An initial FIS structure to tune, options.InitialFIS.

  • Validation data for preventing overfitting to training data, options.ValidationData.

  • Training algorithm options, such as the maximum number of training epochs, options.EpochNumber, or the training error goal, options.ErrorGoal.

  • Whether to display training progress information, such as the training error values for each training epoch, options.DisplayErrorValues.

Output Arguments

collapse all

Trained fuzzy inference system with membership function parameters tuned using the training data, returned as a mamfis or sugfis object. This fuzzy system corresponds to the epoch for which the training error is smallest. If two epochs have the same minimum training error, the FIS from the earlier epoch is returned.

Root mean square training error for each training epoch, returned as an array. The minimum value in trainError is the training error for fuzzy system fis.

Training step size for each epoch, returned as an array. The anfis training algorithm tunes the FIS parameters using gradient descent optimization methods. The training step size is the magnitude of the gradient transitions in the parameter space.

Ideally, the step size increases at the start of training, reaches a maximum, and then decreases for the remainder of the training. To achieve this step size profile, adjust the initial step size (options.InitialStepSize), step size increase rate (options.StepSizeIncreaseRate), and step size decrease rate options.StepSizeDecreaseRate.

Tuned FIS for which the validation error is minimum, returned as a mamfis or sugfis object. If two epochs have the same minimum validation error, the FIS from the earlier epoch is returned.

chkFIS is returned only when you specify validation data using options.ValidationData.

Root mean square training error, returned as an array with length equal to the number of training epochs. The minimum value in chkError is the training error for fuzzy system chkFIS.

chkError is returned only when you specify validation data using options.ValidationData.

Alternative Functionality

tunefis Function

Starting in R2019a, you can tune a fuzzy system using tunefis. This function provides several other options for tuning algorithms, specified by the tunefisOptions object.

To use ANFIS, specify the tuning algorithm as "anfis" in tunefisOptions. Then, use the options object as an input argument for tunefis. For example:

Create the initial fuzzy inference system, and define the tunable parameter settings.

x = (0:0.1:10)';
y = sin(2*x)./exp(x/5);
options = genfisOptions('GridPartition');
options.NumMembershipFunctions = 5;
fisin = genfis(x,y,options);
[in,out,rule] = getTunableSettings(fisin);

Tune the membership function parameters with "anfis".

opt = tunefisOptions("Method","anfis");
fisout = tunefis(fisin,[in;out],x,y,opt);

Fuzzy Logic Designer App

Starting in R2023a, you can interactively tune an ANFIS system using the Fuzzy Logic Designer app. For an example, see Train Adaptive Neuro-Fuzzy Inference Systems.

References

[1] Jang, J.-S. R. "Fuzzy Modeling Using Generalized Neural Networks and Kalman Filter Algorithm." Proceedings of the Ninth National Conference on Artificial Intelligence (AAAI-91). (July 1991): 762–767.

[2] Jang, J.-S. R. "ANFIS: Adaptive-Network-based Fuzzy Inference Systems." IEEE Transactions on Systems, Man, and Cybernetics 23, no. 3 (May 1993): 665–685.

Version History

Introduced before R2006a

expand all