主要内容

incrementalLearner

R2026b

Convert neural network regression model to incremental learner

Since R2026b

Description

IncrementalMdl = incrementalLearner(Mdl) returns a neural network regression model for incremental learning, IncrementalMdl, using the traditionally trained neural network model in Mdl.

The property values of IncrementalMdl reflect the knowledge gained from Mdl (parameters and hyperparameters of the model). Therefore, IncrementalMdl can predict labels given new observations, and it is warm, meaning that its predictive performance is tracked.

example

IncrementalMdl = incrementalLearner(Mdl,Name=Value) uses additional options specified by one or more name-value arguments. Some options require you to train IncrementalMdl before its predictive performance is tracked. For example, MetricsWarmupPeriod=50,MetricsWindowSize=100 specifies a preliminary incremental training period of 50 observations before performance metrics are tracked, and specifies processing 100 observations before updating the window performance metrics.

example

Examples

collapse all

Train a neural network regression model by using fitrnet, and then convert it to an incremental learner.

Load and Preprocess Data

Load the 2015 NYC housing data set. For more details on the data, see NYC Open Data.

load NYCHousing2015

Extract the response variable SALEPRICE from the table. For numerical stability, scale SALEPRICE by 1e6.

Y = NYCHousing2015.SALEPRICE/1e6;
NYCHousing2015.SALEPRICE = [];

To reduce computational cost for this example, remove the NEIGHBORHOOD column, which contains a categorical variable with 254 categories.

NYCHousing2015.NEIGHBORHOOD = [];

Create dummy variable matrices from the other categorical predictors.

catvars = ["BOROUGH","BUILDINGCLASSCATEGORY"];
dumvarstbl = varfun(@(x)dummyvar(categorical(x)),NYCHousing2015, ...
    InputVariables=catvars);
dumvarmat = table2array(dumvarstbl);
NYCHousing2015(:,catvars) = [];

Treat all other numeric variables in the table as predictors of sales price. Concatenate the matrix of dummy variables to the rest of the predictor data.

idxnum = varfun(@isnumeric,NYCHousing2015,OutputFormat="uniform");
X = [dumvarmat NYCHousing2015{:,idxnum}];

Train Neural Network Regression Model

Fit a neural network regression model to the entire data set. Standardize the predictor data.

Mdl = fitrnet(X,Y,Standardize=true)
Mdl = 
  RegressionNeuralNetwork
             ResponseName: 'Y'
    CategoricalPredictors: []
        ResponseTransform: 'none'
          NumObservations: 91446
               LayerSizes: 10
              Activations: 'relu'
    OutputLayerActivation: 'none'
                   Solver: 'LBFGS'
          ConvergenceInfo: [1×1 struct]
          TrainingHistory: [1000×7 table]


  Properties, Methods

Mdl is a RegressionNeuralNetwork model object representing a traditionally trained neural network regression model.

Convert Trained Model

Convert the traditionally trained neural network regression model to a model for incremental learning.

IncrementalMdl = incrementalLearner(Mdl)
IncrementalMdl = 
  incrementalRegressionNeuralNetwork

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


  Properties, Methods

IncrementalMdl is an incrementalRegressionNeuralNetwork model object prepared for incremental learning.

  • The incrementalLearner function initializes the incremental learner by passing model parameters to it, along with other information Mdl extracted from the training data.

  • IncrementalMdl is not warm (IsWarm is 0), which means that incremental learning functions do not start tracking performance metrics.

Predict Responses

An incremental learner created from converting a traditionally trained model can generate predictions without further processing.

Predict sales prices for all observations using both models.

ttyfit = predict(Mdl,X);
ilyfit = predict(IncrementalMdl,X);
compareyfit = norm(ttyfit - ilyfit)
compareyfit = 
0

The difference between the fitted values generated by the models is 0.

Use a trained neural network regression model to initialize an incremental learner. Prepare the incremental learner by specifying a metrics warm-up period and a metrics window size.

Load the robot arm data set.

load robotarm

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

Randomly partition the data into 5% and 95% sets: the first set for training a model traditionally, and the second set for incremental learning.

n = numel(ytrain);

rng(1) % For reproducibility
cvp = cvpartition(n,Holdout=0.95);
idxtt = training(cvp);
idxil = test(cvp);

% 5% set for traditional training
Xtt = Xtrain(idxtt,:);
Ytt = ytrain(idxtt);

% 95% set for incremental learning
Xil = Xtrain(idxil,:);
Yil = ytrain(idxil);

Fit a neural network regression model to the first set.

TTMdl = fitrnet(Xtt,Ytt);

Convert the traditionally trained neural network regression model to a model for incremental learning. Specify the following:

  • A performance metrics warm-up period of 2000 observations.

  • A metrics window size of 500 observations.

  • Use of MSE and mean absolute error (MAE) to measure the performance of the model. The software supports MSE. Create an anonymous function that measures the absolute error of each new observation. Create a structure array containing the name MeanAbsoluteError and its corresponding function.

maefcn = @(z,zfit)abs(z - zfit);
maemetric = struct(MeanAbsoluteError=maefcn);
IncrementalMdl = incrementalLearner(TTMdl,MetricsWarmupPeriod=2000,MetricsWindowSize=500, ...
    Metrics={"mse",maemetric});

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

  • Simulate a data stream by processing 50 observations at a time.

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

  • Store the cumulative metrics, window metrics, and number of training observations to see how they evolve during incremental learning.

% Preallocation
nil = numel(Yil);
numObsPerChunk = 50;
nchunk = floor(nil/numObsPerChunk);
mse = array2table(zeros(nchunk,2),VariableNames=["Cumulative","Window"]);
mae = array2table(zeros(nchunk,2),VariableNames=["Cumulative","Window"]);
numtrainobs = [IncrementalMdl.NumTrainingObservations; zeros(nchunk+1,1)];

% Incremental fitting
for j = 1:nchunk
    ibegin = min(nil,numObsPerChunk*(j-1) + 1);
    iend   = min(nil,numObsPerChunk*j);
    idx = ibegin:iend;    
    IncrementalMdl = updateMetricsAndFit(IncrementalMdl,Xil(idx,:),Yil(idx));
    mse{j,:} = IncrementalMdl.Metrics{"MeanSquaredError",:};
    mae{j,:} = IncrementalMdl.Metrics{"MeanAbsoluteError",:};
    numtrainobs(j+1) = IncrementalMdl.NumTrainingObservations;
end

IncrementalMdl is an incrementalRegressionNeuralNetwork 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.

Plot a trace plot of the number of training observations and the performance metrics on separate tiles.

t = tiledlayout(4,1);
nexttile
plot(numtrainobs)
xlim([0 nchunk])
xline(IncrementalMdl.TrainingOptions.TuningPeriod/numObsPerChunk,"b--")
ylabel(["Number of Training","Observations"])
nexttile
plot(mse.Variables)
xlim([0 nchunk])
ylabel("MSE")
xline((IncrementalMdl.TrainingOptions.TuningPeriod + ...
    IncrementalMdl.MetricsWarmupPeriod)/numObsPerChunk,"r--")
nexttile
plot(mae.Variables)
xlim([0 nchunk])
ylabel("MAE")
xline((IncrementalMdl.TrainingOptions.TuningPeriod + ...
    IncrementalMdl.MetricsWarmupPeriod)/numObsPerChunk,"r--")
xlabel(t,"Iteration")

Figure contains 3 axes objects. Axes object 1 with ylabel Number of Training Observations contains 2 objects of type line, constantline. Axes object 2 with ylabel MSE contains 3 objects of type line, constantline. Axes object 3 with ylabel MAE contains 3 objects of type line, constantline.

The plot suggests that updateMetricsAndFit does the following:

  • Fit the model during all incremental learning iterations after the solver tuning period.

  • Compute the performance metrics after the solver tuning period and metrics warm-up period only.

  • Compute the cumulative metrics during each iteration.

  • Compute the window metrics after processing 500 observations.

Input Arguments

collapse all

Traditionally trained neural network regression model, specified as a RegressionNeuralNetwork model object returned by fitrnet.

Note

Incremental learning functions support only numeric input predictor data. If Mdl was trained on categorical data, you must prepare an encoded version of the categorical data to use incremental learning functions. Use dummyvar to convert each categorical variable to a numeric matrix of dummy variables. Then, concatenate all dummy variable matrices and any other numeric predictors, in the same way that the training function encodes categorical data. For more details, see Dummy Variables.

Name-Value Arguments

collapse 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: incrementalLearner(Mdl,MetricsWindowSize=100) specifies processing 100 observations before updating the window performance metrics.

Model performance metrics to track during incremental learning with updateMetrics and updateMetricsAndFit, specified as "mse", a function handle (@metricName), a structure array of function handles, or a cell vector of function handles or structure arrays. The default setting ("mse") is the weighted mean squared error.

For more information, see loss.

Example: Metrics=@myMetricsFun

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

metric = customMetric(Y,YFit)

  • 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 select the function name (customMetric).

  • Y is a length n numeric vector of observed responses, where n is the sample size.

  • YFit is a length n numeric vector of corresponding predicted responses.

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 'mse' struct(Metric3=@customMetric3)}

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

Metrics Value Data TypeDescription of Metrics Property Row NameExample
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 @(Y,YFit)customMetric(Y,YFit)... 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. 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.

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

Example: MetricsWindowSize=250

Data Types: single | double

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

Example: TrainingOptions=incrementalTrainingOptions("freerex")

Output Arguments

collapse all

Neural network regression model for incremental learning, returned as an incrementalRegressionNeuralNetwork model object. IncrementalMdl is also configured to generate predictions given new data (see predict).

The incrementalLearner function initializes IncrementalMdl for incremental learning using the model information in Mdl. The following table shows the Mdl properties that incrementalLearner passes to corresponding properties of IncrementalMdl.

PropertyDescription
LayerSizesSizes of fully connected layers
ActivationsActivation functions for fully connected layers
LayerWeightsTrained layer weight matrices,
LayerWeightsInitializerInitialization method for layer weights (stored in Mdl.ModelParameters)
LayerBiasesTrained layer bias vectors
LayerBiasesInitializerInitialization method for layer biases (stored in Mdl.ModelParameters)
MuPredictor variable means
SigmaPredictor variable standard deviations
NumPredictorsNumber of predictors (inferred from the X property of Mdl)
LambdaRegularization term strength (stored in Mdl.ModelParameters and passed to IncrementalMdl.L2Regularization)
ResponseMeanMean of response variable. The function passes this property when StandardizeResponses=true.
ResponseStandardDeviationStandard deviation of response variable. The function passes this property when StandardizeResponses=true.

If you specify TrainingOptions, the function passes to IncrementalMdl.L2Regularization the L2Regularization property value of the incremental training options object (default value = 1e-5) instead of the Lambda property value of Mdl.

The function always sets the EstimationPeriod property of IncrementalMdl to 0. The incremental model uses the Mu and Sigma values of Mdl to standardize the predictor data. If Mu and Sigma are empty, the predictor data is not standardized.

When you create Mdl, 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.

You can specify Solver and TuningPeriod using the TrainingOptions name-value argument.

More About

collapse all

Version History

Introduced in R2026b