Main Content

Classify and Update Network State in Simulink

This example shows how to classify data for a trained recurrent neural network in Simulink® by using theStateful Classifyblock. This example uses a pretrained long short-term memory (LSTM) network.

负载Pretrained网络

LoadJapaneseVowelsNet, a pretrained long short-term memory (LSTM) network trained on the Japanese Vowels data set as described in [1] and [2]. This network was trained on the sequences sorted by sequence length with a mini-batch size of 27.

loadJapaneseVowelsNet

View the network architecture.

analyzeNetwork(net);

Load Test Data

Load the Japanese Vowels test data.XTestis a cell array containing 370 sequences of dimension 12 of varying length.TTestis a categorical vector of labels "1","2",..."9", which correspond to the nine speakers.

Create a timetable arraysiminwith time-stamped rows and repeated copies ofX.

[XTest,TTest] = japaneseVowelsTestData; X = XTest{94}; numTimeSteps = size(X,2); simin = timetable(repmat(X,1,4)','TimeStep',seconds(0.2));

Simulink Model for Classifying Data

The Simulink model for classifying data contains aStateful Classifyblock to predict the labels andFrom Workspaceblock to load the input data sequence over the time steps.

To reset the state of recurrent neural network to its initial state during simulation, place theStateful Classifyblock inside aResettable Subsystemand use theResetcontrol signal as trigger.

open_system('StatefulClassifyExample');

Configure Model for Simulation

Set the model configuration parameters for theStateful Classifyblock.

set_param('StatefulClassifyExample/Stateful Classify','NetworkFilePath','JapaneseVowelsNet.mat'); set_param('StatefulClassifyExample',“SimulationMode”,'Normal');

Run the Simulation

To compute responses for theJapaneseVowelsNet网络运行imulation. The prediction labels are saved in the MATLAB® workspace.

out = sim('StatefulClassifyExample');

Plot the predicted labels in a stair plot. The plot shows how the predictions change between time steps.

labels = squeeze(out.YPred.Data(1:numTimeSteps,1)); figure stairs(labels,'-o') xlim([1 numTimeSteps]) xlabel("Time Step") ylabel("Predicted Class") title("Classification Over Time Steps")

Compare the predictions with the true label. Plot a horizontal line showing the true label of the observation.

trueLabel = double(TTest(94)); holdonline([1 numTimeSteps],[trueLabel trueLabel],...'Color',“红色”,...'LineStyle','--') legend(["Prediction""True Label"]) axis([1 numTimeSteps+1 0 9]);

References

[1] M. Kudo, J. Toyama, and M. Shimbo. "Multidimensional Curve Classification Using Passing-Through Regions."Pattern Recognition Letters. Vol. 20, No. 11–13, pages 1103–1111.

[2]UCI Machine Learning Repository: Japanese Vowels Dataset. https://archive.ics.uci.edu/ml/datasets/Japanese+Vowels

See Also

|||

Related Topics