%% Predict and Update Network State
% Predict responses using a trained 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. Predict the scores of each time step and update the network state.

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

% Plot the prediction scores. The plot shows how the prediction scores change between time steps.

classNames = string(net.Layers(end).Classes);
figure
lines = plot(scores');
xlim([1 numTimeSteps])
legend("Class " + classNames,'Location','northwest')
xlabel("Time Step")
ylabel("Score")
title("Prediction Scores Over Time Steps")

% Highlight the prediction scores over time steps for the correct class.

trueLabel = YTest(94)
lines(trueLabel).LineWidth = 3;

% Display the final time step prediction in a bar chart.

figure
bar(score)
title("Final Prediction Scores")
xlabel("Class")
ylabel("Score")

% Copyright 2019 The MathWorks, Inc.