main content

perform incremental learning and track performance metrics using update metrics block -凯发k8网页登录

since r2023b

this example shows how to use the , , and blocks to perform incremental learning and track performance metrics in simulink®. the incrementalclassificationlinear fit block fits a chunk of observations (predictor data) using a configured incremental linear model for binary classification () and outputs updated incremental learning model parameters as a bus signal. the incrementalclassificationlinear predict block accepts an incrementalclassificationlinear model and a chunk of predictor data, and outputs the predicted labels for the observations. the update metrics block predicts labels for a chunk of predictor data using the incremental learning model, and outputs performance metrics.

load and preprocess data

load the human activity data set. randomly shuffle the data.

load humanactivity
ndata = numel(actid);
rng(0,"twister") % for reproducibility
idx = randsample(ndata,ndata);
x = feat(idx,:);
y = actid(idx);

for details on the data set, enter description at the command line.

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;

select 10,000 observations as the training set, and select 10,000 observations to track the model performance metrics.

n = 10000;
xtrain = x(1:n,:);
ytrain = y(1:n,:);
xmetrics = x(n 1:2*n,:);
ymetrics = y(n 1:2*n,:);

create incremental learner model

create an incremental linear model for binary classification. specify that the data has 60 predictors and that the data type of the responses is logical. also specify to standardize the data using an estimation period of 500 observations before the update metrics block outputs performance metrics. create a workspace variable linearmdl to store the initial incremental learning model.

mdl = incrementalclassificationlinear(classnames=[false,true], ...
    numpredictors=60,standardize=true,estimationperiod=500, ...
    metricswarmupperiod=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 to import into the incrementalclassificationlinear predict block.

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) = xtrain(idx,:);
    yin(:,j) = ytrain(idx);
    xm_in(:,:,j) = xmetrics(idx,:);
    ym_in(:,j) = ymetrics(idx);
    xtest(1,:,j) = xtrain(idx(1),:);
 end

convert the training, metrics, and test set chunks into time series objects.

t = 0:size(xin,3)-1;
xtrain_ts = timeseries(xin,t,interpretsinglerowdataas3d=true);
ytrain_ts = timeseries(yin',t,interpretsinglerowdataas3d=true);
xm_ts = timeseries(xm_in,t,interpretsinglerowdataas3d=true);
ym_ts = timeseries(ym_in',t,interpretsinglerowdataas3d=true);
xtest_ts = timeseries(xtest,t,interpretsinglerowdataas3d=true);

open provided simulink model

this example provides the simulink model slexupdatemetricsexample.slx, which includes the incrementalclassificationlinear fit, incrementalclassificationlinear predict, and update metrics blocks. the simulink model is configured to use linearmdl as the initial model for incremental learning and classification.

open the simulink model slexupdatemetricsexample.slx.

slname = "slexupdatemetricsexample";
open_system(slname);

simulate model

simulate the simulink model to perform incremental learning, predict responses for the test set observations, and compute performance metrics. export the simulation outputs to the workspace. you can use the simulation data inspector (simulink) to view the logged data of an outport block.

simout = sim(slname,"stoptime",num2str(numel(t)-1));
% extract labels
yfit_sig = simout.yout.getelement(1);
yfit_sl = squeeze(yfit_sig.values.data);
% extract score values
scores_sig = simout.yout.getelement(2);
scores_sl = squeeze(scores_sig.values.data);
% extract beta values
beta_sig = simout.yout.getelement(3);
beta_sl = squeeze(beta_sig.values.data);
% extract bias values
bias_sig = simout.yout.getelement(4);
bias_sl = squeeze(bias_sig.values.data);
% extract iswarm values
iswarm_sig = simout.yout.getelement(5);
iswarm_sl = squeeze(iswarm_sig.values.data);
% extract metrics values
metrics_sig = simout.yout.getelement(6);
metrics_sl = squeeze(metrics_sig.values.data);

at each iteration, the incrementalclassificationlinear fit block trains the model and updates the model parameters. the incrementalclassificationlinear predict block calculates the predicted label for the test set observation, and the update metrics block calculates the performance metrics.

analyze model during training

to see how the model parameters and scores evolve during training, plot them on separate tiles.

figure
tiledlayout(3,1);
nexttile
plot(scores_sl(1,:),".")
ylabel("scores")
xlabel("iteration")
xlim([0 nchunk])
nexttile
plot(beta_sl(1,:),".-")
ylabel("\beta_1")
xlabel("iteration")
xlim([0 nchunk])
nexttile
plot(bias_sl,".-")
ylabel("bias")
xlabel("iteration")
xlim([0 nchunk])

figure contains 3 axes objects. axes object 1 with xlabel iteration, ylabel scores contains a line object which displays its values using only markers. axes object 2 with xlabel iteration, ylabel \beta_1 contains an object of type line. axes object 3 with xlabel iteration, ylabel bias contains an object of type line.

during the estimation period, the incrementalclassificationlinear fit block estimates hyperparameters but does not fit the initial model (see ). therefore, the score output of the incrementalclassificationlinear predict block, model beta coefficients, and model bias all equal 0 during the first 10 iterations. at the end of the estimation period, the incrementalclassificationlinear fit block updates the model parameters and predicts labels. the score values vary between approximately –10 and 5. the first beta coefficient drops significantly during the first 30 iterations following the estimation period, and then varies between –0.14 and –0.07 thereafter. the bias (intercept) term fluctuates initially and then gradually approaches 0.04.

plot the iswarm status indicator value and cumulative performance metric on separate tiles.

figure
tiledlayout(2,1);
nexttile
plot(iswarm_sl,".")
ylabel("iswarm")
xlabel("iteration")
xlim([0 nchunk])
nexttile
plot(metrics_sl(:,1),".")
ylabel("cumulative metric (classiferror)")
xlabel("iteration")
xlim([0 nchunk])
ylim([0 0.005])

figure contains 2 axes objects. axes object 1 with xlabel iteration, ylabel iswarm contains a line object which displays its values using only markers. axes object 2 with xlabel iteration, ylabel cumulative metric (classiferror) contains a line object which displays its values using only markers.

after the estimation period, the update metrics block fits observations during the metrics warm-up period but does not calculate performance metrics. after the metrics warm-up period, iswarm = 1 and the block calculates the performance metrics. in the provided simulink model, the model performance metric is classiferror (the classification error). the cumulative classification error is –1 during the first 21 iterations, 0 for the next 23 iterations, and then varies between 0.001 and 0.003.

网站地图