Main Content

Configure Simulink Template for Rate-Based Incremental Linear Classification

Since R2024a

This example shows how to configure the Simulink® Rate-Based Incremental Learning template to perform incremental linear classification. Create and train a binary linear classification model for incremental learning. Create input data to simulate streaming data for the Simulink Inport blocks. Load the template and configure it for incremental linear classification. Configure the rate transition blocks to specify when the model predicts responses and updates 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, ...
    ClassNames=[false,true],Standardize=true,EstimationPeriod=500);
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),:);
 end

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 Rate-Based Incremental Learning template. The template is also available on the Simulink Start Page under Statistics and Machine Learning.

template = Simulink.createFromTemplate("multirate_linear_classif.sltx");
open_system(template)

Adjust Sample Times

You can specify how often Simulink updates the performance metrics of the model. Double-click the Update Metrics block, enter 4 in the Sample time dialog box and click OK. You can also specify how often Simulink predicts responses from the test data. Double-click the IncrementalRegressionLinear Predict block, enter 2 in the Sample time dialog box and click OK.

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.

set_param(gcs,LoadExternalInput="on")
set_param(gcs,ExternalInput="X_ts,Y_ts,X_ts,Y_ts,Xtest_ts")

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= ...
         "["+num2str(numObsPerChunk)+","+num2str(p)+"]")
    set_param(xNamePath,OutDataTypeStr="double")
end

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

set_param([gcs,'/x2'],PortDimensions="["+'1'+","+num2str(p)+"]")
set_param([gcs,'/x2'],OutDataTypeStr="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_param(yNamePath,PortDimensions=num2str(numObsPerChunk))
    set_param(yNamePath,OutDataTypeStr="boolean")
end

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

set_param(gcs,StopTime=num2str(k*5))

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