Main Content

Configure Simulink Template for Conditionally Enabled Incremental Linear Classification

Since R2024a

This example shows how to configure the Simulink® Enabled Execution for Incremental Learning template to perform incremental linear classification. The template is set up for linear regression, but you can also configure it for classification models. Create and train a binary classification linear model for incremental learning. Edit the blocks in the template and prepare data for the input ports. Configure the conditional logic subsystem block, which determines when Simulink fits the model and updates the performance metrics. Run the model in Simulink to predict labels and calculate classification scores.

Load and Preprocess Data

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

load humanactivity
n = numel(actid); % Number of observations
p = size(feat,2); % Number of predictors
rng(0,"twister") % For reproducibility
idx = randsample(n,n);
X = feat(idx,:);
Y = actid(idx);

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

The data has 60 predictors (p) and 24,075 observations (n). Responses can be one of five classes: Sitting, Standing, Walking, Running, or Dancing. Dichotomize the response by identifying whether the subject is moving (actid > 2).

Y = Y > 2;

Create Incremental Learning Model

Create an incremental linear model for binary classification. Specify that the data has p predictors and that the data type of the responses is logical. Specify to standardize the data using an estimation period of 500 observations. Create a workspace variable linearMdl to store the initial incremental learning model.

Mdl = incrementalClassificationLinear(NumPredictors=p, ...
linearMdl = Mdl;

Create Input Data for Simulink

Simulate streaming data by dividing the training data into chunks of 50 observations. For each chunk, select a single observation as a test set.

numObsPerChunk = 50;
nchunk = floor(n/numObsPerChunk);
for j = 1:nchunk
    ibegin = min(n,numObsPerChunk*(j-1) + 1);
    iend = min(n,numObsPerChunk*j);
    idx = ibegin:iend;   
    Xin(:,:,j) = X(idx,:);
    Yin(:,j) = Y(idx);
    Xtest(1,:,j) = X(idx(1),:);

Convert the training and test set chunks into time series objects.

k = size(Xin,3); % Number of data chunks
t = 0:k-1;
X_ts = timeseries(Xin,t,InterpretSingleRowDataAs3D=true);
Y_ts = timeseries(Yin',t,InterpretSingleRowDataAs3D=true);
Xtest_ts = timeseries(Xtest,t,InterpretSingleRowDataAs3D=true);

Load and Configure Template to Perform Incremental Linear Classification

Load the Enabled Execution for Incremental Learning template. The template is also available on the Simulink Start Page under Statistics and Machine Learning.

template = Simulink.createFromTemplate("enabled_linear_regr.sltx");

The template is configured for incremental linear regression. Edit the template to configure it for incremental linear classification:

  1. Double-click the Incremental Fit subsystem block.

  2. Delete the IncrementalRegressionLinear Fit block.

  3. Add an IncrementalClassificationLinear Fit block and connect it to the existing Inport and Outport blocks.

  4. Click the Up to Parent arrow button to return to the main model canvas and delete the IncrementalRegressionLinear Predict block.

  5. Add an IncrementalClassificationLinear Predict block and double-click it to open the Block Parameters dialog box.

  6. Select the Add output port for predicted class scores check box to add the second output port score. Click OK.

  7. Connect the block to the existing Inport and Outport blocks.

  8. Change the name of the yfit Outport to labels, and change the name of the canPredict Outport to scores.

Configure Conditional Logic Subsystem

Double-click the Conditional Logic subsystem block. You can modify the sample conditional logic model, which determines when the system fits the incremental learning model and calculates performance metrics. For example, you can change the triggering levels for window loss and cumulative loss, and whether to use the IsWarm and CanPredict properties of the incremental learning model when triggering.

Configure Simulink Model Parameters

Click the Simulink template canvas to select it as the current system. The template contains five Inport blocks: x, y, x1, y1, and x2. Specify to enable external input and to use the streaming data X_ts and Y_ts as inputs to the Incremental Fit subsystem block and the Update Metrics block. Use the test data Xtest_ts as the input to the IncrementalClassificationLinear Predict block.


Specify the port dimensions of the predictor data Inport blocks for the Incremental Fit block and Update Metrics block (x and x1) as [numObsPerChunk,p], and specify their output data type as double.

xPortNames = {'/x','/x1'};
for indX = 1:numel(xPortNames)
    xNamePath = [gcs,xPortNames{indX}];
    set_param(xNamePath,PortDimensions= ...

Specify the port dimensions of the predictor data Inport block x2 as [1,p], and specify its output data type as double.


Specify the port dimensions of the response data Inport blocks (y and y1) as [numObsPerChunk,p], and specify their output data type as Boolean.

yPortNames = {'/y','/y1'};
for indY = 1:numel(yPortNames)
    yNamePath = [gcs,yPortNames{indY}];

Set the simulation stop time to the number of data chunks times the number of Inport blocks.


Simulate Model

Click the Run button in the Simulink model to perform incremental learning, predict responses to the test set observations, and calculate classification scores. You can use the Simulation Data Inspector (Simulink) to view the logged data of the Outport blocks.

See Also

| | | | |

Related Topics