Hauptinhalt

incrementalClassificationNeuralNetwork

R2026b

Neural network classification model for incremental learning

Since R2026b

Description

The incrementalClassificationNeuralNetwork function creates an incrementalClassificationNeuralNetwork model object, which represents a neural network classification model for incremental learning.

Unlike other Statistics and Machine Learning Toolbox™ model objects, incrementalClassificationNeuralNetwork can be called directly. Also, you can specify learning options, such as performance metrics configurations and the objective solver, before fitting the model to data. After you create an incrementalClassificationNeuralNetwork object, it is prepared for incremental learning.

incrementalClassificationNeuralNetwork is best suited for incremental learning. For a traditional approach to training a neural network model for classification (such as creating a model by fitting it to data, performing cross-validation, tuning hyperparameters, and so on), see fitcnet.

Creation

You can create an incrementalClassificationNeuralNetwork model object in several ways:

  • Call the function directly — Configure incremental learning options, or specify learner-specific options, by calling incrementalClassificationNeuralNetwork directly. This approach is best when you do not have data yet or you want to start incremental learning immediately.

  • Convert a traditionally trained model — To initialize a model for incremental learning using the model parameters and hyperparameters of a trained model object, you can convert the traditionally trained model (ClassificationNeuralNetwork) to an incrementalClassificationNeuralNetwork model object by passing it to the incrementalLearner function.

  • Call an incremental learning function — fit, updateMetrics, and updateMetricsAndFit accept a configured incrementalClassificationNeuralNetwork model object and data as input, and return an incrementalClassificationNeuralNetwork model object updated with information learned from the input model and data.

Description

Mdl = incrementalClassificationNeuralNetwork(Name=Value) returns a default incremental learning model object for neural network classification, Mdl, and sets properties and other options using name-value arguments. For example, incrementalClassificationNeuralNetwork(MaxNumClasses=5,LayerSizes=[50 30]) specifies a model with a maximum of five classes and two fully connected layers of size 50 and 30.

Properties of a default model contain placeholders for unknown model parameters. You must train a default model before you can track its performance or generate predictions from it.

When you call incrementalClassificationNeuralNetwork, you must specify at least one of the following name-value arguments or name-value argument combinations.

  • MaxNumClasses — Maximum number of classes expected in the response data during incremental learning

  • ClassNames — List of all class names expected in the response data during incremental learning

  • LayerWeights, LayerBiases, and Activations — Weights, biases, and activation functions of the fully connected layers

example

Name-Value Arguments

expand all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: incrementalClassificationNeuralNetwork(MaxNumClasses=5,NumPredictors=7) specifies a model with a maximum of five classes and seven predictors.

Classification Options

expand all

Maximum number of classes expected in the response data during incremental learning, specified as a positive integer.

If you specify ClassNames, then MaxNumClasses must equal the number of class names.

Example: MaxNumClasses=5

Data Types: single | double

Names of the classes in the response variable, specified as a categorical array, string array, logical vector, numeric vector, or cell array of character vectors. ClassNames and the response data must have the same data type. This argument sets the ClassNames property.

ClassNames specifies the order of any input or output argument dimension that corresponds to the class order. For example, set ClassNames to specify the order of the dimensions of Cost and the column order of classification scores returned by predict.

If you do not specify ClassNames, the software infers the ClassNames property from the data during incremental learning.

Example: ClassNames=["cat","dog","bird"]

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

Prior class probabilities, specified as "empirical" (the default), "uniform", or a numeric vector of nonnegative values. If you do not specify ClassNames, then Prior must be "empirical". This argument sets the Prior property. incrementalClassificationNeuralNetwork stores the Prior value as a numeric vector.

ValueDescription
"empirical"Incremental learning functions infer prior class probabilities from the observed class relative frequencies in the response data during incremental training.
"uniform"For each class, the prior probability is 1/K, where K is the number of classes.
numeric vector of nonnegative valuesCustom, normalized prior probabilities. The order of the elements of Prior corresponds to the elements of the ClassNames property.

Example: Prior=[0.3 0.3 0.4]

Data Types: single | double | char | string

Score transformation function describing how incremental learning functions transform raw response values, specified as a character vector, string scalar, or function handle. This argument sets the ScoreTransform property. incrementalClassificationNeuralNetwork stores the specified value as a character vector or function handle.

This table describes the available built-in functions for score transformation.

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 + e–x)
"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 + e–x) – 1

For a MATLAB® function or a function that you define, enter its function handle; for example, @function, where:

  • function accepts an n-by-K matrix (the original scores) and returns a matrix of the same size (the transformed scores).

  • n is the number of observations, and row j of the matrix contains the class scores of observation j.

  • K is the number of classes, and column k is class ClassNames(k).

The default ScoreTransform value depends on how you create the model:

  • If you convert a traditionally trained model to create Mdl, ScoreTransform is specified by the corresponding property of the traditionally trained model.

  • The default "none" specifies returning posterior class probabilities.

Example: ScoreTransform="logit"

Data Types: char | string | function handle

Cost of misclassifying an observation, specified as a value in the following table, where c is the number of classes in the ClassNames property. This argument sets the Cost property.

ValueDescription
c-by-c numeric matrix

Cost(i,j) is the cost of classifying an observation into class j when its true class is i, for classes ClassNames(i) and ClassNames(j). In other words, the rows correspond to the true class and the columns correspond to the predicted class. For example, Cost = [0 2;1 0] applies double the penalty for misclassifying ClassNames(1) than for misclassifying ClassNames(2).

Structure array

A structure array having two fields:

  • ClassNames containing the class names, the same value as ClassNames

  • ClassificationCosts containing the cost matrix, as previously described.

If you specify Cost, you must also specify the ClassNames argument.

The default is one of the following alternatives:

  • An empty array [] when you specify MaxNumClasses

  • A c-by-c matrix when you specify ClassNames, where Cost(i,j) = 1 for all i ≠ j, and Cost(i,j) = 0 for all i = j

Example: Cost=struct(ClassNames=["b","g"],ClassificationCosts=[0 2; 1 0])

Data Types: single | double | struct

Neural Network Options

expand all

Activation functions for the fully connected layers of the neural network model, specified as one of the following values. The activation function for the final fully connected layer is always softmax. This argument sets the Activations property.

  • String scalar or character vector — Use the specified activation function for each of the fully connected layers of the model, excluding the final fully connected layer. For more information, see Neural Network Structure.

  • String array or cell array of character vectors — Use the ith element of Activations for the ith fully connected layer of the model. You cannot specify the activation function of the final fully connected layer.

Specify the activation functions using one or more of these values:

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,x≥00,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+e−x

"none"

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

If you specify Activations, you must also specify either LayerSizes, or LayerWeights and LayerBiases.

Example: Activations="sigmoid"

Example: Activations=["relu","tanh"]

Data Types: char | string

Output sizes of the fully connected layers in the neural network model, specified as a positive integer vector. This argument sets the LayerSizes property. The ith element of LayerSizes is the number of outputs in the ith fully connected layer of the network. You cannot specify the output size of the final connected layer, which has an output size equal to the number of classes. You cannot specify LayerSizes when you specify LayerWeights.

Example: LayerSizes=[50 30]

Data Types: single | double

Weights for the fully connected layers, specified as a cell array of numeric matrices. This argument sets the LayerWeights property. The number of cell elements must equal the number of fully connected layers (numel(LayerSizes) + 1). The ith element contains the weight matrix for the ith fully connected layer. The first dimension of the last cell element determines the number of classes, and the second dimension of the first cell element determines the number of predictors. Layer weights are typically set during training or when converting from a traditionally trained model. You must specify LayerWeights, LayerBiases, and Activations together.

Data Types: cell

Initialization method for the layer weights, specified as one of these values:

  • "glorot" — Initialize the weights with the Glorot initializer [1] (also known as the Xavier initializer). For each layer, the Glorot initializer independently samples from a uniform distribution with zero mean and variance 2/(I+O), where I is the input size and O is the output size for the layer.

  • "he" — Initialize the weights with the He initializer [2]. For each layer, the He initializer samples from a normal distribution with zero mean and variance 2/I, where I is the input size for the layer.

The reset function uses the LayerWeightsInitializer function to initialize the layer weights.

Example: LayerWeightsInitializer="he"

Data Types: string | char

Biases for the fully connected layers, specified as a cell array of numeric column vectors. This argument sets the LayerBiases property. The number of cell elements must equal the number of layers (numel(LayerSizes) + 1). The ith element contains the weight matrix for the ith fully connected layer. The first dimension of the last cell element determines the number of classes, and the second dimension of the first cell element determines the number of predictors. Layer biases are typically set during training or when converting from a traditionally trained model. You must specify LayerWeights, LayerBiases, and Activations together.

Data Types: cell

Initialization method for layer biases, specified as one of these values:

  • "zeros" — Initialize the biases with a vector of zeros.

  • "ones" — Initialize the biases with a vector of ones.

The reset function uses the LayerBiasesInitializer method to initialize the layer biases.

Example: LayerBiasesInitializer="ones"

Data Types: string | char

Training Parameters

expand all

Solver training options, specified as a TrainingOptionsMiniBatchLBFGS or TrainingOptionsFREEREX object returned by incrementalTrainingOptions. The training options specify the solver algorithm and its hyperparameters. This argument sets the TrainingOptions property. For more information about solver algorithms, see the Limited-Memory BFGS and FreeRex sections of the incrementalTrainingOptions reference page.

Example: TrainingOptions=incrementalTrainingOptions("freerex")

Flag to standardize the predictor data, specified as a numeric or logical 0 (false) or 1 (true). If you set Standardize to true, then the software centers and scales each numeric predictor variable by the corresponding column mean and standard deviation.

If you specify Standardize=true and do not specify EstimationPeriod, the function sets the EstimationPeriod property value to 1000.

Example: Standardize=true

Data Types: logical

Number of predictor variables, specified as a nonnegative integer. This argument sets the NumPredictors property.

The default NumPredictors value depends on how you create the model:

  • If you convert a traditionally trained model to create Mdl, then NumPredictors is specified by the corresponding property of the traditionally trained model.

  • If you create Mdl by calling incrementalClassificationNeuralNetwork directly, you can specify NumPredictors by using name-value argument syntax. If you do not specify the value, then the default value is 0, and the incremental fitting functions infer NumPredictors from the predictor data during training.

Example: NumPredictors=6

Data Types: single | double

Number of observations processed by the incremental fitting functions fit and updateMetricsAndFit to estimate the predictor means and standard deviations, specified as a nonnegative integer. This argument sets the EstimationPeriod property.

If you specify Standardize=true, the default value is 1000.

If you specify a positive value of EstimationPeriod when you create Mdl:

  • The software sets EstimationPeriod=0 unless you specify Standardize=true.

  • The incremental fitting functions use the first incoming EstimationPeriod observations to estimate the predictor means (Mu) and standard deviations (Sigma) prior to training the model.

  • The software ignores observations that contain at least one missing value when processing observations during the estimation period.

  • Estimation occurs only when Mdl.Mu is [] or an array of zeros, and Mdl.Sigma is [] or an array of ones.

  • At the end of the estimation period, the software updates the Mu and Sigma properties of the model.

Example: EstimationPeriod=500

Data Types: single | double

Performance Metrics Options

expand all

Model performance metrics to track during incremental learning, in addition to minimal expected misclassification cost, specified as a built-in loss function name, string vector of names, function handle (for example, @metricName), structure array of function handles, or cell vector of names, function handles, or structure arrays. This argument sets the Metrics property.

When Mdl is warm (see IsWarm), the updateMetrics and updateMetricsAndFit functions track performance metrics in the Metrics property of Mdl.

The following table lists the built-in loss function names. You can specify more than one by using a string vector.

NameMetrics Property LabelDescription
"binodeviance"BinomialDevianceBinomial deviance
"classiferror"ClassificationErrorMisclassification error rate
"exponential"ExponentialLossExponential
"hinge"HingeLossHinge
"logit"LogitLossLogistic
"mincost"MinimalCost

Minimal expected misclassification cost (for classification scores that are posterior probabilities). incrementalClassificationNeuralNetwork always tracks this metric.

"quadratic"QuadraticLossQuadratic
"crossentropy"CrossEntropyLossCross-entropy

For more details on the built-in loss functions, see loss.

Example: Metrics=["classiferror" "logit"]

To specify a custom function that returns a performance metric, use function handle notation. The function must have this form.

metric = customMetric(C,S,Cost)

  • The output argument metric is an n-by-1 numeric vector, where each element is the loss of the corresponding observation in the data processed by the incremental learning functions during a learning cycle.

  • You specify the function name (here, customMetric).

  • C is an n-by-K logical matrix with rows indicating the class to which the corresponding observation belongs, where K is the number of classes. The column order corresponds to the class order in the ClassNames property. Create C by setting C(p,q) = 1, if observation p is in class q, for each observation in the specified data. Set the other element in row p to 0.

  • S is an n-by-K numeric matrix of predicted classification scores. S is similar to the Posterior output of predict, where rows correspond to observations in the data and the column order corresponds to the class order in the ClassNames property. S(p,q) is the classification score of observation p being classified in class q.

  • Cost is a K-by-K numeric matrix of misclassification costs. See the Cost name-value argument.

To specify multiple custom metrics and assign a custom name to each, use a structure array. To specify a combination of built-in and custom metrics, use a cell vector.

Example: Metrics=struct(Metric1=@customMetric1,Metric2=@customMetric2)

Example: Metrics={@customMetric1 @customMetric2 "logit" struct(Metric3=@customMetric3)}

updateMetrics and updateMetricsAndFit store specified metrics in a table in the Metrics property. The data type of Metrics determines the row names of the table.

"Metrics" Value Data TypeDescription of Metrics Property Row NameExample
String or character vectorName of corresponding built-in metricRow name for "classiferror" is "ClassificationError"
Structure arrayField nameRow name for struct(Metric1=@customMetric1) is "Metric1"
Function handle to function stored in a program fileName of functionRow name for @customMetric is "customMetric"
Anonymous functionCustomMetric_j, where j is metric j in MetricsRow name for @(C,S,Cost)customMetric(C,S,Cost)... is CustomMetric_1

For more details on performance metrics options, see Performance Metrics.

Data Types: char | string | struct | cell | function_handle

Number of observations to fit during the metrics warm-up period, specified as a nonnegative integer. This argument sets the MetricsWarmupPeriod property. The metrics warm-up period takes place after the solver tuning period and estimation period (if specified). The metrics warm-up period is completed when the incremental fitting functions have processed MetricsWarmupPeriod observations and at least one observation from each expected class. After the metrics warm-up period, the model object is warm and the incremental fitting functions compute and store performance metrics.

For more details, see Incremental Training Periods.

Example: MetricsWarmupPeriod=50

Data Types: single | double

Number of observations to use to compute window performance metrics, specified as a positive integer. This argument sets the MetricsWindowSize property.

For more details on performance metrics options, see Performance Metrics.

Example: MetricsWindowSize=250

Data Types: single | double

Properties

expand all

You can set most properties by using name-value pair argument syntax only when you call incrementalClassificationNeuralNetwork directly. You can set some properties when you call incrementalLearner to convert a traditionally trained model. You cannot set the properties IsWarm, Mu, Sigma, OutputLayerActivation, and NumTrainingObservations.

Classification Model Properties

This property is read-only after object creation.

All unique class labels expected in the response data during incremental learning, specified as a cell array of character vectors.

Data Types: cell

This property is read-only after object creation.

Cost of misclassifying an observation, specified as an array.

If you specify Cost, its value sets Cost. If you specify a structure array, then Cost is the value of the ClassificationCosts field.

If you convert a traditionally trained model to create Mdl, then Cost is the Cost property of the traditionally trained model.

Data Types: double

This property is read-only after object creation.

Number of predictor variables, specified as a nonnegative numeric scalar.

The default NumPredictors value depends on how you create the model:

  • If you convert a traditionally trained model to create Mdl, NumPredictors is specified by the corresponding property of the traditionally trained model.

  • If you create Mdl by calling incrementalClassificationNeuralNetwork directly, you can specify NumPredictors by using name-value argument syntax. If you do not specify the value, then the default value is 0, and incremental fitting functions infer NumPredictors from the predictor data during training.

Data Types: double

This property is read-only after object creation.

Prior class probabilities, specified as "empirical", "uniform", or a numeric vector. incrementalClassificationNeuralNetwork stores the Prior value as a numeric vector.

ValueDescription
"empirical"Incremental learning functions infer prior class probabilities from the observed class relative frequencies in the response data during incremental training.
"uniform"For each class, the prior probability is 1/K, where K is the number of classes.
numeric vectorCustom, normalized prior probabilities. The order of the elements of Prior corresponds to the elements of the ClassNames property.

The default Prior value depends on how you create the model:

  • If you convert a traditionally trained model to create Mdl, Prior is specified by the corresponding property of the traditionally trained model.

  • Otherwise, the default value is "empirical".

Data Types: double

This property is read-only after object creation.

Score transformation function describing how incremental learning functions transform raw response values, specified as a character vector, string scalar, or function handle. incrementalClassificationNeuralNetwork stores the specified value as a character vector or function handle.

This table describes the available built-in functions for score transformation.

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 + e–x)
"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 + e–x) – 1

For a MATLAB function or a function that you define, enter its function handle; for example, @function, where:

  • function accepts an n-by-K matrix (the original scores) and returns a matrix of the same size (the transformed scores).

  • n is the number of observations, and row j of the matrix contains the class scores of observation j.

  • K is the number of classes, and column k is class ClassNames(k).

The default ScoreTransform value depends on how you create the model:

  • If you convert a traditionally trained model to create Mdl, ScoreTransform is specified by the corresponding property of the traditionally trained model.

  • The default "none" specifies returning posterior class probabilities.

Data Types: char | function_handle

Training Properties

This property is read-only.

Predictor means, represented as a numeric vector.

  • When you create Mdl and specify NumPredictors=0 or Standardize=false (the default), then Mu is an empty array [].

  • When you create Mdl and set Standardize=true, and Mu is [] or an array of zeros, then the incremental fit function calculates the predictor variable means using all data points that do not have any missing values. At the end of the estimation period specified by EstimationPeriod, Mu is a NumPredictors-by-1 vector that contains the predictor means.

Data Types: double

This property is read-only.

Predictor standard deviations, represented as a numeric vector.

  • When you create Mdl and specify NumPredictors=0 or Standardize=false (the default), then Sigma is an empty array [].

  • When you create Mdl and set Standardize=true, and Sigma is [] or an array of zeros, then the incremental fit function calculates the predictor variable standard deviations using all data points that do not have any missing values. At the end of the estimation period specified by EstimationPeriod, Sigma is a NumPredictors-by-1 vector that contains the predictor standard deviations.

Data Types: double

This property is read-only after object creation.

Number of observations processed by the incremental model to estimate the predictor means and standard deviations, represented as a nonnegative integer. If you specify Standardize=true when you create Mdl, the default value is 1000. Otherwise, the default value is 0.

If EstimationPeriod > 0:

  • The incremental fitting functions use EstimationPeriod observations to estimate the predictor means (Mu) and standard deviations (Sigma) prior to training the model.

  • The software ignores observations that contain at least one missing value when processing observations during the estimation period.

  • The estimation period takes place before the solver tuning period and the metrics warm-up period (if specified).

For more information, see Incremental Training Periods.

Data Types: double

This property is read-only after object creation.

Objective function minimization technique, specified as one of the following values:

ValueSolver NameMore Information
"minibatch-lbfgs"Mini-Batch Limited-memory Broyden–Fletcher–Goldfarb–Shanno (LBFGS)

Limited-Memory BFGS

"freerex"FreeRexFreeRex

If you convert a traditionally trained model to create Mdl, then Solver is "minibatch-lbfgs".

Data Types: string

This property is read-only after object creation.

Solver training options, specified as a TrainingOptionsMiniBatchLBFGS or TrainingOptionsFREEREX object. If you convert a traditionally trained model to create Mdl, the TrainingOptions name-value argument of the incrementalLearner function sets this property.

This property is read-only.

Number of observations fit to the incremental model Mdl, represented as a nonnegative numeric scalar. NumTrainingObservations increases when you pass Mdl and training data to fit or updateMetricsAndFit.

Note

If you convert a traditionally trained model to create Mdl, incrementalClassificationNeuralNetwork does not add the number of observations fit to the traditionally trained model to NumTrainingObservations.

Data Types: double

Performance Metrics Properties

Flag indicating whether the incremental model tracks performance metrics in the Metrics property, specified as logical 0 (false) or 1 (true).

When you create Mdl with the incrementalClassificationNeuralNetwork function, the model is warm(IsWarm is true) when the following are true:

When you create Mdl with the incrementalLearner function, the model is warm when MetricsWarmupPeriod is 0 and either of the following is true:

  • Solver is "freerex"

  • Solver is "minibatch-lbfgs" and Mdl.TrainingOptions.TuningPeriod is 0.

Otherwise, the incremental model becomes warm after the estimation period, solver tuning period, and metrics warm-up period (if specified). For more information, see Incremental Training Periods.

Data Types: logical

Model performance metrics updated during incremental learning by updateMetrics and updateMetricsAndFit, specified as a table with two columns.

The table contains a row for the MinimalCost metric, and a row for each metric specified by the Metrics name-value argument. The supported metrics are as follows:

LabelMetrics Argument Value
MinimalCost"mincost"
ClassificationError"classiferror"
HingeLoss"hinge"
QuadraticLoss"quadratic"
BinomialDeviance"binodeviance"
ExponentialLoss"exponential"
LogitLoss"logit"
CrossEntropyLoss"crossentropy"

The columns of Metrics are labeled Cumulative and Window.

  • Cumulative: Element j is the model performance, as measured by metric j, from the time the model became warm (IsWarm is 1).

  • Window: Element j is the model performance, as measured by metric j, evaluated over all observations within the window specified by the MetricsWindowSize property. The software updates Window after it processes MetricsWindowSize observations.

If you convert a traditionally trained model to create Mdl, the Metrics name-value argument of the incrementalLearner function sets this property.

Data Types: table

This property is read-only after object creation.

Number of observations in the metrics warm-up period, specified as a nonnegative integer.

If you convert a traditionally trained model to create Mdl, the MetricsWindowSize name-value argument of the incrementalLearner function sets this property.

For more details about the metrics warm-up period, see Incremental Training Periods.

Data Types: double

This property is read-only after object creation.

Number of observations to use to compute window performance metrics, specified as a positive integer.

The default MetricsWindowSize value depends on how you create the model:

  • If you convert a traditionally trained model to create Mdl, the MetricsWindowSize name-value argument of the incrementalLearner function sets this property. The default value of the argument is 200.

  • Otherwise, the default value is 200.

For more details on performance metrics options, see Performance Metrics.

Data Types: double

Neural Network Properties

This property is read-only after object creation.

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

Data Types: double

This property is read-only after object creation.

Weights for the fully connected layers, specified as a cell array of numeric matrices. The ith element contains the weight matrix for the ith fully connected layer.

Data Types: cell

This property is read-only after object creation.

Biases for the fully connected layers, specified as a cell array of numeric column vectors. The ith element contains the weight matrix for the ith fully connected layer.

Data Types: cell

This property is read-only after object creation.

Activation functions for the fully connected layers of the neural network model, specified as a string or a string array containing one or more of the following values. The activation function for the final fully connected layer is always softmax.

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,x≥00,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+e−x

"none"

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

Data Types: string

This property is read-only after object creation.

Activation function for the final fully connected layer, specified as "softmax". The softmax 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).

Object Functions

fitTrain neural network model for incremental learning
updateMetricsUpdate performance metrics in neural network incremental learning model given new data
updateMetricsAndFitUpdate performance metrics in neural network incremental learning model given new data and train model
lossLoss of neural network incremental learning model on batch of data
perObservationLossPer observation classification error of model for incremental learning
predictPredict responses for new observations from neural network incremental learning model
resetReset incremental classification model
dlnetwork (Deep Learning Toolbox)Deep learning neural network

Examples

collapse all

When you create a neural network classification model for incremental learning, you can specify the maximum number of classes that you expect the model to process (MaxNumClasses name-value argument). As you fit the model to incoming batches of data by using an incremental fitting function, the model collects new classes in its ClassNames property. If the specified maximum number of classes is inaccurate, one of the following occurs:

  • Before an incremental fitting function processes the expected maximum number of classes, the model is not warm. Consequently, the updateMetrics and updateMetricsAndFit functions do not measure performance metrics.

  • If the number of classes exceeds the maximum expected, the incremental fitting function issues an error.

This example shows how to create a neural network classification model for incremental learning when the only information you specify is the expected maximum number of classes in the data. Also, the example illustrates the consequences when incremental fitting functions process all expected classes early and late in the sample.

For this example, consider training a device to predict whether a subject is sitting, standing, walking, running, or dancing based on biometric data measured on the subject. Therefore, the device has a maximum of five classes from which to choose.

Process Expected Maximum Number of Classes Early in Sample

Create an incremental neural network model for multiclass learning. Specify a maximum of five classes in the data, and standardize the predictor values.

MdlEarly = incrementalClassificationNeuralNetwork(MaxNumClasses=5)
MdlEarly = 
  incrementalClassificationNeuralNetwork

                   IsWarm: 0
                  Metrics: [1×2 table]
               ClassNames: [1×0 double]
           ScoreTransform: 'none'
               LayerSizes: 10
              Activations: "relu"
    OutputLayerActivation: "softmax"
                   Solver: "minibatch-lbfgs"


  Properties, Methods

MdlEarly is an incrementalClassificationNeuralNetwork model object. MdlEarly must be fit to data before you can use it to perform any other operations.

Display the default training period values associated with the model object.

MdlEarly.TrainingOptions.TuningPeriod
ans = 
1000
MdlEarly.MetricsWarmupPeriod
ans = 
1000

When you use fit and updateMetricsAndFit to fit the model, these functions:

  • Use the first incoming 1000 observations to tune the initial learning rate for the solver

  • Process the next 1000 observations during the warm-up period

Once the model has been fit to all expected classes and at least 2000 observations, the model is warm, and the fit and updateMetricsAndFit functions compute and store performance metrics.

Load the human activity data set. Randomly shuffle the data.

load humanactivity
n = numel(actid);
rng(1); % For reproducibility
idx = randsample(n,n);
X = feat(idx,:);
Y = actid(idx);

For details on the data set, enter Description at the command line.

Fit the incremental model to the training data by using the updateMetricsAndFit function. Simulate a data stream by processing chunks of 50 observations at a time. At each iteration:

  • Process 50 observations.

  • Overwrite the previous incremental model with a new one fitted to the incoming observations.

  • Store the cumulative metrics and the window metrics to see how they evolve during incremental learning.

% Preallocation
numObsPerChunk = 50;
nchunk = floor(n/numObsPerChunk);
mc = array2table(zeros(nchunk,2),VariableNames=["Cumulative" "Window"]);
IsWarm = zeros(nchunk+1,1);    
% Incremental learning
for j = 1:nchunk
    ibegin = min(n,numObsPerChunk*(j-1) + 1);
    iend = min(n,numObsPerChunk*j);
    idx = ibegin:iend;    
    MdlEarly = updateMetricsAndFit(MdlEarly,X(idx,:),Y(idx));
    mc{j,:} = MdlEarly.Metrics{"MinimalCost",:};
    IsWarm(j + 1) = MdlEarly.IsWarm;
end

MdlEarly is an incrementalClassificationNeuralNetwork model object trained on all the data in the stream. During incremental learning and after the model is warm, updateMetricsAndFit checks the performance of the model on the incoming observations, and then fits the model to those observations.

To see how the IsWarm property and performance metrics evolve during training, plot them on separate tiles.

t = tiledlayout(2,1);
nexttile
plot(IsWarm)
ylabel("IsWarm")
xlim([0 nchunk])
ylim([0 1.1])
xline((MdlEarly.TrainingOptions.TuningPeriod + ...
    MdlEarly.MetricsWarmupPeriod)/numObsPerChunk,"r-.")
nexttile
h = plot(mc.Variables);
xlim([0 nchunk])
ylabel("Minimal Cost")
xline((MdlEarly.TrainingOptions.TuningPeriod + ...
    MdlEarly.MetricsWarmupPeriod)/numObsPerChunk,"r-.")
legend(h,mc.Properties.VariableNames)
xlabel(t,"Iteration")

Figure contains 2 axes objects. Axes object 1 with ylabel IsWarm contains 2 objects of type line, constantline. Axes object 2 with ylabel Minimal Cost contains 3 objects of type line, constantline. These objects represent Cumulative, Window.

The plots indicate that updateMetricsAndFit performs the following actions:

  • Compute the performance metrics after the tuning and metrics warm-up periods (red vertical line) only.

  • Compute the cumulative metrics during each iteration.

  • Compute the window metrics after processing 200 observations (4 iterations).

Process Expected Maximum Number of Classes Late in Sample

Create a different neural network model for incremental learning for the objective.

MdlLate = incrementalClassificationNeuralNetwork(MaxNumClasses=5, ...
    Standardize=true)
MdlLate = 
  incrementalClassificationNeuralNetwork

                   IsWarm: 0
                  Metrics: [1×2 table]
               ClassNames: [1×0 double]
           ScoreTransform: 'none'
               LayerSizes: 10
              Activations: "relu"
    OutputLayerActivation: "softmax"
                   Solver: "minibatch-lbfgs"


  Properties, Methods

Move all observations labeled with class 5 to the end of the sample.

idx5 = Y == 5;
Xnew = [X(~idx5,:); X(idx5,:)];
Ynew = [Y(~idx5) ;Y(idx5)];

Fit the incremental model and plot the results.

mcnew = array2table(zeros(nchunk,2),VariableNames=["Cumulative" "Window"]);

for j = 1:nchunk
    ibegin = min(n,numObsPerChunk*(j-1) + 1);
    iend   = min(n,numObsPerChunk*j);
    idx = ibegin:iend;    
    MdlLate = updateMetricsAndFit(MdlLate,Xnew(idx,:),Ynew(idx));
    mcnew{j,:} = MdlLate.Metrics{"MinimalCost",:};
 end

figure
h = plot(mcnew.Variables);
xlim([0 nchunk]);
ylabel("Minimal Cost")
xline((MdlLate.TrainingOptions.TuningPeriod + ...
    MdlLate.MetricsWarmupPeriod)/numObsPerChunk,"r-.")
xline(sum(~idx5)/numObsPerChunk,"g-.")
legend(h,mcnew.Properties.VariableNames,Location="best")
xlabel("Iteration")

Figure contains an axes object. The axes object with xlabel Iteration, ylabel Minimal Cost contains 4 objects of type line, constantline. These objects represent Cumulative, Window.

The updateMetricsAndFit function trains the model throughout incremental learning, but the function starts tracking performance metrics only after the model is fit to all expected number of classes (the green vertical line).

Create an incremental neural network model when you know all the class names in the data.

Consider training a device to predict whether a subject is sitting, standing, walking, running, or dancing based on biometric data measured on the subject. The class names map 1 through 5 to an activity.

Create an incremental neural network model for multiclass learning. Specify the class names.

classnames = 1:5;
Mdl = incrementalClassificationNeuralNetwork(ClassNames=classnames)
Mdl = 
  incrementalClassificationNeuralNetwork

                   IsWarm: 0
                  Metrics: [1×2 table]
               ClassNames: [1 2 3 4 5]
           ScoreTransform: 'none'
               LayerSizes: 10
              Activations: "relu"
    OutputLayerActivation: "softmax"
                   Solver: "minibatch-lbfgs"


  Properties, Methods

Mdl is an incrementalClassificationNeuralNetwork model object.

Mdl must be fit to data before you can use it to perform any other operations.

Load the human activity data set. Randomly shuffle the data.

load humanactivity
n = numel(actid);
rng(1) % For reproducibility
idx = randsample(n,n);
X = feat(idx,:);
Y = actid(idx);

For details on the data set, enter Description at the command line.

Fit the incremental model to the training data by using the updateMetricsAndFit function. Simulate a data stream by processing chunks of 50 observations at a time. At each iteration:

  • Process 50 observations.

  • Overwrite the previous incremental model with a new one fitted to the incoming observations.

% Preallocation
numObsPerChunk = 50;
nchunk = floor(n/numObsPerChunk);

% Incremental learning
for j = 1:nchunk
    ibegin = min(n,numObsPerChunk*(j-1) + 1);
    iend   = min(n,numObsPerChunk*j);
    idx = ibegin:iend;    
    Mdl = updateMetricsAndFit(Mdl,X(idx,:),Y(idx));
end

Load the human activity data set. Randomly shuffle the data.

load humanactivity
n = numel(actid);
idx = randsample(n,n);
X = feat(idx,:);
Y = actid(idx);

Fit an initial classification neural network model object to the first 1000 observations using the fitcnet function..

Mdl = fitcnet(X(1:1000,:),Y(1:1000));

Create an incrementalClassificationNeuralnetwork model object by specifying the network layer activations, weights, and biases from the initial model.

incrMdl = incrementalClassificationNeuralNetwork(Activations=Mdl.Activations, ...
    LayerWeights=Mdl.LayerWeights,LayerBiases=Mdl.LayerBiases)
incrMdl = 
  incrementalClassificationNeuralNetwork

                   IsWarm: 0
                  Metrics: [1×2 table]
               ClassNames: [1×0 double]
           ScoreTransform: 'none'
               LayerSizes: 10
              Activations: "relu"
    OutputLayerActivation: "softmax"
                   Solver: "minibatch-lbfgs"


  Properties, Methods

The model object incrMdl contains the network parameters from the initial model and is ready for incremental learning using the fit and updateMetricsAndFit functions.

Load the human activity data set. Randomly shuffle the data.

load humanactivity
n = numel(actid);
rng(0,"twister"); % For reproducibility
idx = randsample(n,n);
X = feat(idx,:);
Y = actid(idx);

The class names map 1 through 5 to an activity—sitting, standing, walking, running, or dancing, respectively—based on biometric data measured on the subject. For details on the data set, enter Description at the command line.

Create an incremental neural network model for multiclass learning. Configure the model as follows:

  • Specify a metrics warm-up period of 5000 observations.

  • Specify a metrics window size of 500 observations.

  • Standardize the predictor data and specify an estimation period of 1000 observations.

  • Use the mini-batch LBFGS solver and a solver tuning period of 500 observations.

  • Double the penalty to the classifier when it mistakenly classifies class 2.

  • Track the classification error and minimal cost to measure the performance of the model. You do not have to specify mincost for Metrics because incrementalClassificationNeuralNetwork always tracks this metric.

C = ones(5) - eye(5);
C(2,[1 3 4 5]) = 2;
Mdl = incrementalClassificationNeuralNetwork(ClassNames=1:5, ...
    MetricsWarmupPeriod=5000,MetricsWindowSize=500, ...
    Standardize=true,EstimationPeriod=1000, ...
    TrainingOptions=incrementalTrainingOptions("minibatch-lbfgs", ...
    TuningPeriod=500),Cost=C,Metrics="classiferror")
Mdl = 
  incrementalClassificationNeuralNetwork

                   IsWarm: 0
                  Metrics: [2×2 table]
               ClassNames: [1 2 3 4 5]
           ScoreTransform: 'none'
               LayerSizes: 10
              Activations: "relu"
    OutputLayerActivation: "softmax"
                   Solver: "minibatch-lbfgs"


  Properties, Methods

Mdl is an incrementalClassificationNeuralNetwork model object configured for incremental learning.

Fit the incremental model to the rest of the data by using the updateMetricsAndFit function. At each iteration:

  • Simulate a data stream by processing a chunk of 50 observations.

  • Overwrite the previous incremental model with a new one fitted to the incoming observations.

  • Store the standard deviation of the first predictor variable σ1, the cumulative metrics, and the window metrics to see how they evolve during incremental learning.

% Preallocation
numObsPerChunk = 50;
nchunk = floor(n/numObsPerChunk);
ce = array2table(zeros(nchunk,2),VariableNames=["Cumulative" "Window"]);
mc = array2table(zeros(nchunk,2),VariableNames=["Cumulative" "Window"]);
sigma1 = zeros(nchunk+1,1);    

% Incremental fitting
for j = 1:nchunk
    ibegin = min(n,numObsPerChunk*(j-1) + 1);
    iend   = min(n,numObsPerChunk*j);
    idx = ibegin:iend;    
    Mdl = updateMetricsAndFit(Mdl,X(idx,:),Y(idx));
    ce{j,:} = Mdl.Metrics{"ClassificationError",:};
    mc{j,:} = Mdl.Metrics{"MinimalCost",:};
    sigma1(j) = Mdl.Sigma(1);
end

Mdl is an incrementalClassificationNeuralNetwork model object trained on all the data in the stream. During incremental learning and after the model is warmed up, updateMetricsAndFit checks the performance of the model on the incoming observations, and then fits the model to those observations.

To see how the performance metrics and σ1 evolve during training, plot them on separate tiles.

tiledlayout(2,2)
nexttile
plot(sigma1)
ylabel("\sigma_{1}")
xlim([0 nchunk]);
xline(Mdl.EstimationPeriod/numObsPerChunk,"b--")
xlabel("Iteration")
nexttile
h = plot(ce.Variables);
xlim([0 nchunk])
ylabel("Classification Error")
xline((Mdl.EstimationPeriod + Mdl.TrainingOptions.TuningPeriod + ...
 Mdl.MetricsWarmupPeriod)/numObsPerChunk,"r-.")
legend(h,ce.Properties.VariableNames)
xlabel("Iteration")
nexttile
h = plot(mc.Variables);
xlim([0 nchunk]);
ylabel("Minimal Cost")
xline((Mdl.EstimationPeriod + Mdl.TrainingOptions.TuningPeriod + ...
    Mdl.MetricsWarmupPeriod)/numObsPerChunk,"r-.")
legend(h,mc.Properties.VariableNames)
xlabel("Iteration")

The plots indicate that updateMetricsAndFit performs the following actions:

  • Fit σ1 after the estimation period (blue vertical line).

  • Compute the performance metrics after the estimation period, tuning period, and metrics warm-up period (red vertical line) only.

  • Compute the cumulative metrics during each iteration.

  • Compute the window metrics after processing 500 observations (10 iterations).

More About

expand all

References

[1] Glorot, Xavier, and Yoshua Bengio. “Understanding the difficulty of training deep feedforward neural networks.” In Proceedings of the thirteenth international conference on artificial intelligence and statistics, pp. 249–256. 2010.

[2] He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. “Delving deep into rectifiers: Surpassing human-level performance on imagenet classification.” In Proceedings of the IEEE international conference on computer vision, pp. 1026–1034. 2015.

Version History

Introduced in R2026b