Main Content

Train Neural Network Classifiers Using Classification Learner App

This example shows how to create and compare neural network classifiers in the Classification Learner app, and export trained models to the workspace to make predictions for new data.

  1. In the MATLAB®Command Window, load thefisheririsdata set, and create a table from the variables in the data set to use for classification.

    fishertable = readtable("fisheriris.csv");
  2. Click theAppstab, and then click theShow morearrow on the right to open the apps gallery. In theMachine Learning and Deep Learninggroup, clickClassification Learner.

  3. On theClassification Learnertab, in theFile部分, clickNew Sessionand selectFrom Workspace.

    Classification Learner tab

  4. In the New Session from Workspace dialog box, select the tablefishertablefrom theData Set Variable列表(如果有必要)。观察到app希利cted response and predictor variables based on their data types. Petal and sepal length and width are predictors, and species is the response that you want to classify. For this example, do not change the selections.

  5. To accept the default validation scheme and continue, clickStart Session. The default validation option is 5-fold cross-validation, to protect against overfitting.

    Classification Learner creates a scatter plot of the data.

  6. Use the scatter plot to investigate which variables are useful for predicting the response. Select different options in theXandYlists underPredictorsto visualize the distribution of species and measurements. Note which variables separate the species colors most clearly.

  7. Create a selection of neural network models. On theClassification Learnertab, in theModels部分, click the arrow to open the gallery. In theNeural Network Classifiersgroup, clickAll Neural Networks.

  8. In theTrain部分, clickTrain Alland selectTrain All.

    Note

    • If you have Parallel Computing Toolbox™, then the app has theUse Parallelbutton toggled on by default. After you clickTrain Alland selectTrain AllorTrain Selected, the app opens a parallel pool of workers. During this time, you cannot interact with the software. After the pool opens, you can continue to interact with the app while models train in parallel.

    • If you do not have Parallel Computing Toolbox, then the app has theUse Background Trainingcheck box in theTrain Allmenu selected by default. After you click to train models, the app opens a background pool. After the pool opens, you can continue to interact with the app while models train in the background.

    Classification Learner trains one of each neural network classification option in the gallery, as well as the default fine tree model. In theModelspane, the app outlines theAccuracy (Validation)score of the best model. Classification Learner also displays a validation confusion matrix for the first neural network model (Narrow Neural Network).

  9. Select a model in theModelspane to view the results. For example, double-click theNarrow Neural Networkmodel (model 2.1). Inspect the modelSummarytab, which displays theTraining Resultsmetrics, calculated on the validation set.

  10. Examine the scatter plot for the trained model. On theClassification Learnertab, in thePlots部分, click the arrow to open the gallery, and then clickScatterin theValidation Resultsgroup. Correctly classified points are marked with an O, and incorrectly classified points are marked with an X.

    Scatter plot of the Fisher iris data modeled by a neural network classifier

    Note

    Validation introduces some randomness into the results. Your model validation results can vary from the results shown in this example.

  11. Inspect the accuracy of the predictions in each class. On theClassification Learnertab, in thePlots部分, click the arrow to open the gallery, and then clickConfusion Matrix (Validation)in theValidation Resultsgroup. View the matrix of true class and predicted class results.

  12. For each remaining model, select the model in theModelspane, open the validation confusion matrix, and then compare the results across the models.

  13. Choose the best model in theModels窗格(最好的分数被高亮显示Accuracy (Validation)box). See if you can improve the model by removing features with low predictive power.

    First duplicate the best model. On theClassification Learnertab, in theModels部分, clickDuplicate.

  14. To investigate features to include or exclude, use the parallel coordinates plot. On theClassification Learnertab, in thePlots部分, click the arrow to open the gallery, and then clickParallel Coordinatesin theValidation Resultsgroup.

    You can specify the predictors to use during training in the modelSummarytab. ClickFeature Selectionto expand the section, and specify predictors to remove from the model.

  15. Alternatively, you can use a feature ranking algorithm to determine which features to use during model training. On theClassification Learnertab, in theOptions部分, clickFeature Selection. In theDefault Feature Selectiontab, specify the feature ranking algorithm you want to use. Specify the number of features to keep among the highest ranked features. You can use the bar graph to help decide how many features to use.

    ClickSave and Applyto save your changes. The new feature selection is applied to the existing draft model in theModelspane and will be applied to new draft models that you create using the gallery in theModels部分of theClassification Learnertab.

  16. Train the model. On theClassification Learnertab, in theTrain部分, clickTrain Alland selectTrain Selectedto train the model using the new options. Compare results among the classifiers in theModelspane.

  17. Choose the best model in theModelspane. To try to improve the model further, change its hyperparameters. First, duplicate the model using theDuplicatebutton in theModels部分. Then, try changing hyperparameter settings, like the sizes of the fully connected layers or the regularization strength, in the modelSummarytab. Train the new model by clickingTrain Alland selectingTrain Selectedin theTrain部分.

    To learn more about neural network model settings, seeNeural Network Classifiers.

  18. You can export a full or compact version of the trained model to the workspace. On theClassification Learnertab, in theExport部分, clickExport Modeland select eitherExport ModelorExport Compact Model. SeeExport Classification Model to Predict New Data.

  19. To examine the code for training this classifier, clickGenerate Functionin theExport部分.

Tip

Use the same workflow to evaluate and compare the other classifier types you can train in Classification Learner.

To train all the nonoptimizable classifier model presets available for your data set:

  1. On theClassification Learnertab, in theModels部分, click the arrow to open the gallery of models.

  2. In theGet Startedgroup, clickAll. Then, in theTrain部分, clickTrain Alland selectTrain All.

    Option selected for training all available classifier types

To learn about other classifier types, seeTrain Classification Models in Classification Learner App.

Related Topics