%% Classify and Update Network State
% Classify data using a recurrent neural network and update the network state.
% Load JapaneseVowelsNet, 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.

load JapaneseVowelsNet

% View the network architecture.

net.Layers

%
% Load the test data.

[XTest,YTest] = japaneseVowelsTestData;

% Loop over the time steps in a sequence. Classify each time step and update the network state.

X = XTest{94};
numTimeSteps = size(X,2);
for i = 1:numTimeSteps
    v = X(:,i);
    [net,label,score] = classifyAndUpdateState(net,v);
    labels(i) = label;
end

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

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 = YTest(94)
hold on
line([1 numTimeSteps],[trueLabel trueLabel], ...
    'Color','red', ...
    'LineStyle','--')
legend(["Prediction" "True Label"])

% Copyright 2019 The MathWorks, Inc.