Main Content

CompactClassificationNeuralNetwork

Compact neural network model for classification

    Description

    CompactClassificationNeuralNetwork is a compact version of a ClassificationNeuralNetwork model object. The compact model does not include the data used for training the classifier. Therefore, you cannot perform some tasks, such as cross-validation, using the compact model. Use a compact model for tasks such as predicting the labels of new data.

    Creation

    Create a CompactClassificationNeuralNetwork object from a full ClassificationNeuralNetwork model object by using compact.

    Properties

    expand all

    Neural Network Properties

    This property is read-only.

    Sizes of the fully connected layers in the neural network model, returned as a positive integer vector. The ith element of LayerSizes is the number of outputs in the ith fully connected layer of the neural network model.

    LayerSizes does not include the size of the final fully connected layer. This layer always has K outputs, where K is the number of classes in the response variable.

    Data Types: single | double

    This property is read-only.

    Learned layer weights for the fully connected layers, returned as a cell array. The ith entry in the cell array corresponds to the layer weights for the ith fully connected layer. For example, Mdl.LayerWeights{1} returns the weights for the first fully connected layer of the model Mdl.

    LayerWeights includes the weights for the final fully connected layer.

    Data Types: cell

    This property is read-only.

    Learned layer biases for the fully connected layers, returned as a cell array. The ith entry in the cell array corresponds to the layer biases for the ith fully connected layer. For example, Mdl.LayerBiases{1} returns the biases for the first fully connected layer of the model Mdl.

    LayerBiases includes the biases for the final fully connected layer.

    Data Types: cell

    This property is read-only.

    Activation functions for the fully connected layers of the neural network model, returned as a character vector or cell array of character vectors with values from this table.

    ValueDescription
    'relu'

    Rectified linear unit (ReLU) function — Performs a threshold operation on each element of the input, where any value less than zero is set to zero, that is,

    f(x)={x,x00,x<0

    'tanh'

    Hyperbolic tangent (tanh) function — Applies the tanh function to each input element

    'sigmoid'

    Sigmoid function — Performs the following operation on each input element:

    f(x)=11+ex

    'none'

    Identity function — Returns each input element without performing any transformation, that is, f(x) = x

    • If Activations contains only one activation function, then it is the activation function for every fully connected layer of the neural network model, excluding the final fully connected layer. The activation function for the final fully connected layer is always softmax (OutputLayerActivation).

    • If Activations is an array of activation functions, then the ith element is the activation function for the ith layer of the neural network model.

    Data Types: char | cell

    This property is read-only.

    Activation function for the final fully connected layer, returned as 'softmax'. The function takes each input xi and returns the following, where K is the number of classes in the response variable:

    f(xi)=exp(xi)j=1Kexp(xj).

    The results correspond to the predicted classification scores (or posterior probabilities).

    Data Properties

    This property is read-only.

    Predictor variable names, returned as a cell array of character vectors. The order of the elements of PredictorNames corresponds to the order in which the predictor names appear in the training data.

    Data Types: cell

    This property is read-only.

    Categorical predictor indices, returned as a vector of positive integers. Assuming that the predictor data contains observations in rows, CategoricalPredictors contains index values corresponding to the columns of the predictor data that contain categorical predictors. If none of the predictors are categorical, then this property is empty ([]).

    Data Types: double

    This property is read-only.

    Expanded predictor names, returned as a cell array of character vectors. If the model uses encoding for categorical variables, then ExpandedPredictorNames includes the names that describe the expanded variables. Otherwise, ExpandedPredictorNames is the same as PredictorNames.

    Data Types: cell

    This property is read-only.

    Unique class names used in training, returned as a numeric vector, categorical vector, logical vector, character array, or cell array of character vectors. ClassNames has the same data type as the class labels in the response variable used to train the model. (The software treats string arrays as cell arrays of character vectors.) ClassNames also determines the class order.

    Data Types: single | double | categorical | logical | char | cell

    This property is read-only.

    Response variable name, returned as a character vector.

    Data Types: char

    Other Classification Properties

    This property is read-only.

    Misclassification cost, returned as a numeric square matrix, where Cost(i,j) is the cost of classifying a point into class j if its true class is i. The cost matrix always has this form: Cost(i,j) = 1 if i ~= j, and Cost(i,j) = 0 if i = j. The rows correspond to the true class and the columns correspond to the predicted class. The order of the rows and columns of Cost corresponds to the order of the classes in ClassNames.

    Data Types: double

    This property is read-only.

    Prior probabilities for each class, returned as a numeric vector. The order of the elements of Prior corresponds to the elements of ClassNames.

    Data Types: double

    Score transformation, specified as a character vector or function handle. ScoreTransform represents a built-in transformation function or a function handle for transforming predicted classification scores.

    To change the score transformation function to function, for example, use dot notation.

    • For a built-in function, enter a character vector.

      Mdl.ScoreTransform = 'function';

      This table describes the available built-in functions.

      ValueDescription
      'doublelogit'1/(1 + e–2x)
      'invlogit'log(x / (1 – x))
      'ismax'Sets the score for the class with the largest score to 1, and sets the scores for all other classes to 0
      'logit'1/(1 + ex)
      'none' or 'identity'x (no transformation)
      'sign'–1 for x < 0
      0 for x = 0
      1 for x > 0
      'symmetric'2x – 1
      'symmetricismax'Sets the score for the class with the largest score to 1, and sets the scores for all other classes to –1
      'symmetriclogit'2/(1 + ex) – 1

    • For a MATLAB® function or a function that you define, enter its function handle.

      Mdl.ScoreTransform = @function;

      function must accept a matrix (the original scores) and return a matrix of the same size (the transformed scores).

    Data Types: char | function_handle

    Object Functions

    expand all

    limeLocal interpretable model-agnostic explanations (LIME)
    partialDependenceCompute partial dependence
    plotPartialDependenceCreate partial dependence plot (PDP) and individual conditional expectation (ICE) plots
    shapleyShapley values
    edgeClassification edge for neural network classifier
    lossClassification loss for neural network classifier
    marginClassification margins for neural network classifier
    predictClassify observations using neural network classifier
    compareHoldoutCompare accuracies of two classification models using new data
    testckfoldCompare accuracies of two classification models by repeated cross-validation

    Examples

    collapse all

    Reduce the size of a full neural network classifier by removing the training data from the model. You can use a compact model to improve memory efficiency.

    Load the patients data set. Create a table from the data set. Each row corresponds to one patient, and each column corresponds to a diagnostic variable. Use the Smoker variable as the response variable, and the rest of the variables as predictors.

    load patients
    tbl = table(Diastolic,Systolic,Gender,Height,Weight,Age,Smoker);

    Train a neural network classifier using the data. Specify the Smoker column of tbl as the response variable. Specify to standardize the numeric predictors.

    Mdl = fitcnet(tbl,"Smoker","Standardize",true)
    Mdl = 
      ClassificationNeuralNetwork
               PredictorNames: {'Diastolic'  'Systolic'  'Gender'  'Height'  'Weight'  'Age'}
                 ResponseName: 'Smoker'
        CategoricalPredictors: 3
                   ClassNames: [0 1]
               ScoreTransform: 'none'
              NumObservations: 100
                   LayerSizes: 10
                  Activations: 'relu'
        OutputLayerActivation: 'softmax'
                       Solver: 'LBFGS'
              ConvergenceInfo: [1×1 struct]
              TrainingHistory: [36×7 table]
    
    
      Properties, Methods
    
    

    Mdl is a full ClassificationNeuralNetwork model object.

    Reduce the size of the model by using compact.

    compactMdl = compact(Mdl)
    compactMdl = 
      CompactClassificationNeuralNetwork
                   LayerSizes: 10
                  Activations: 'relu'
        OutputLayerActivation: 'softmax'
    
    
      Properties, Methods
    
    

    compactMdl is a CompactClassificationNeuralNetwork model object. compactMdl contains fewer properties than the full model Mdl.

    Display the amount of memory used by each neural network model.

    whos("Mdl","compactMdl")
      Name            Size            Bytes  Class                                                           Attributes
    
      Mdl             1x1             18836  ClassificationNeuralNetwork                                               
      compactMdl      1x1              6663  classreg.learning.classif.CompactClassificationNeuralNetwork              
    

    The full model is larger than the compact model.

    Introduced in R2021a