Main Content

Train Adaptive Neuro-Fuzzy Inference Systems

Since R2023a

This example shows how to create, train, and test a Sugeno-type fuzzy inference system (FIS) using the Fuzzy Logic Designer app. For more information on:

Before R2023a: Interactively tune ANFIS systems using the Neuro-Fuzzy Designer app.

Import Example Data

Training and validating systems using the Fuzzy Logic Designer app requires existing data.

Import the training data sets to the MATLAB® workspace. Each data set has one input and one output.

load anfisTrainingData

The data for this example includes two training data sets and two validation data sets.

  • Training data set 1 with input data trnInput1 and output data trnOutput1

  • Training data set 2 with input data trnInput2 and output data trnOutput2

  • Validation data set 1 with input data valInput1 and output data valOutput1

  • Validation data set 2 with input data valInput2 and output data valOutput2

Generate Initial FIS

Open Fuzzy Logic Designer.

fuzzyLogicDesigner

To create an initial FIS structure based on your training data, in the Getting Started window, select Generate rules automatically, and click FIS from Data.

Getting Started dialog - In the top-right corner, the Generate rules automatically parameter is selected. The FIS from Data option is highlighted.

In the Create System from Data dialog box:

  • In the Input data drop-down list, select the training input data trnInput1.

  • In the Output data drop-down list, select the training output data trnOutput1.

  • In the Clustering method drop-down list, select Grid partition.

  • In the Input membership function type drop-down list, select Generalized bell.

  • To set the number of input membership functions, in the Number field, enter 4.

  • In the Output membership function type drop-down list, select Linear.

Create System from Data dialog box configured with the previously specified settings.

To create a FIS with the specified structure, click OK.

Interactively Specify FIS Structure

Alternatively, you can interactively specify your own FIS structure with specified membership functions and rules. The system you define must be a Sugeno system with the following properties:

  • Single output

  • Weighted average defuzzification

  • First or zeroth order system; that is, all output membership functions must be the same type, either linear or constant.

  • No rule sharing. Different rules cannot use the same output membership function; that is, the number of output membership functions must equal the number of rules.

  • Unity weight for each rule.

  • No custom membership functions or defuzzification methods.

For more information on building a FIS structure in the app, see Build Fuzzy Systems Using Fuzzy Logic Designer.

Select Data for Training

To select data for tuning, on the Tuning tab:

  • In the Input Data drop-down list, under Imported Data Sets, select trnInput1.

  • In the Output Data drop-down list, under Imported Data Sets, select trnOutput1.

Tuning tab toolstrip highlighting the Input Data and Output Data drop-down lists with trnInput1 and trnOutput1 selected, respectively.

Train FIS

To train your FIS using the selected data, first specify the tuning options. Click Tuning Options.

In the Tuning Options dialog box, in the Method drop-down list, select Adaptive neuro-fuzzy inference system.

To modify the default training options, clear the Use default method options parameter.

Under Method Options: Adaptive neuro-fuzzy inference system, specify the following options.

  • To specify the maximum number of training epochs, set Epoch number to 40.

  • Set the error stopping condition Error goal to 0. Doing so indicates that the training does not stop until the maximum number of training epochs complete.

  • Use the default training method by setting Optimization method to Least squares estimation with backpropagation. This method tunes the FIS parameters using a combination of backpropagation and least-squares regression.

  • Specify validation data for training. During training, the ANFIS algorithm uses the validation data to prevent overfitting.

    • In the Input validation data drop-down list, under Workspace Data Sets, select valInput1.

    • In the Output validation data drop-down list, under Workspace Data Sets, select valOutput1.

  • Keep the remaining training options at their default values.

Tuning options dialog box configured for ANFIS tuning using the previously specified settings.

Click OK.

To train the FIS, on the Tuning tab, click Tune.

The Tune tab shows the training progress.

  • The Convergence Plot document plots the optimization cost (training error) after each epoch for both the training and validation data.

  • The Convergence Results document shows the ANFIS system properties as well as the training error and minimum root mean-squared error results for the training and validation data sets.

Tune tab showing tuning results. The document on the left is a plot showing that the training error decreases over 40 epochs. The document in the middle shows more tuning details including the final minimum training and validation RMSE values of around 0.08 and 0.13, respectively.

The validation error decreases up to a certain point in the training, and then it increases. This increase occurs at the point where the training starts overfitting the training data. The app selects the FIS associated with this overfitting point as the trained ANFIS model.

To accept the training results, click Accept.

The app adds the tuned FIS fis_tuned to the Design Browser pane and sets this FIS as the active design.

Design Browser table containing two entries, the original FIS in the first row and the tuned FIS in the second row.

Validate Trained FIS

Once you train your FIS, you can validate its performance against the training validation data.

To validate only the tuned FIS, in the Design Browser, clear the Compare column entry for the initial system fis.

Design tab toolstrip highlighting the Input Data and Output Data drop-down lists in the Simulation section with valInput1 and valOutput1 selected, respectively.

Next select the input/output data to use for system validation. On the Design tab, in the Simulation section:

  • In the Input Data drop-down list, under Imported Data Sets, select valInput1.

  • In the Output Data drop-down list, under Imported Data Sets, select valOutput1.

Design Browser showing the clear checkbox for the original untuned FIS.

Then, click System Validation.

The System Validation document plots the selected simulation data along with the output of the trained FIS. To get a better view of the output data plot, in the Reference Inputs table, clear the entry in the Select column. For this example the plot legend interferes with viewing the data. To hide the legend, clear the Show Legends parameter.

System validation plot showing a single plot of the original and tuned FIS outputs, which are well correlated.

In the validation plot, the reference output is blue and the tuned FIS output is red. The FIS output correlates well with the reference output.

Once you train and validate your FIS, you can export the FIS and your simulation results to the MATLAB® workspace. For more information, see Export FIS and Simulation Data from Fuzzy Logic Designer.

Importance of Checking Data

It is important to have validation data that fully represents the features of the data the FIS is intended to model. If your checking data is significantly different from your training data and does not cover the same data features to model as the training data, then the training results will be poor.

For example, load new training and validation data into Fuzzy Logic Designer. This data has significantly different training and validation sets. On the Tuning tab:

  • In the Input Data drop-down list, under Workspace Data Sets select trnInput2.

  • In the Output Data drop-down list, under Workspace Data Sets, select trnOutput2.

Click Tuning Options. In the Tuning Options dialog box:

  • In the Input validation data drop-down list, under Workspace Data Sets, select valInput2.

  • In the Output validation data drop-down list, under Workspace Data Sets, select valOutput2.

Click OK.

Before tuning, set the active system to the original initial FIS structure. In the Design Browser pane, select the fis entry in the table and click Set Active Design.

The Active column of the Design Browser shows that the original FIS in the first row is selected.

On the Tuning tab, click Tune.

Training convergence plot sowing a validation error that is significantly larger than the training error.

In this case, the validation error is large, with the minimum occurring in the first epoch. Since the app chooses the trained FIS parameters associated with the minimum validation error, the trained FIS does not sufficiently capture the features of this data set. It is important to know the features of your data set well when you select your training and validation data. When you do not know the features of your data, you can analyze the validation error plots to see whether or not the validation data performed sufficiently well with the trained model.

To verify the poor training results, test the trained FIS model against the validation data.

Click Accept. Then, on the Design tab:

  • In the Input Data drop-down list, under Imported Data Sets select valInput2.

  • In the Output Data drop-down list, under Imported Data Sets, select valOutput2.

Click System Validation.

System validation plot showing a single plot of the original and tuned FIS outputs, which are not well correlated.

As expected, there are significant differences between the validation data output values and the FIS output.

See Also

Related Topics