%% Apply LSTM Operation to Sequence Data
% Perform an LSTM operation using three hidden units.
% Create the input sequence data as 32 observations with 10 channels and a sequence length of 64

numFeatures = 10;
numObservations = 32;
sequenceLength = 64;

X = randn(numFeatures,numObservations,sequenceLength);
dlX = dlarray(X,'CBT');

% Create the initial hidden and cell states with three hidden units. Use the same initial hidden state and cell state for all observations.

numHiddenUnits = 3;
H0 = zeros(numHiddenUnits,1);
C0 = zeros(numHiddenUnits,1);

% Create the learnable parameters for the LSTM operation.

weights = dlarray(randn(4*numHiddenUnits,numFeatures),'CU');
recurrentWeights = dlarray(randn(4*numHiddenUnits,numHiddenUnits),'CU');
bias = dlarray(randn(4*numHiddenUnits,1),'C');

% Perform the LSTM calculation

[dlY,hiddenState,cellState] = lstm(dlX,H0,C0,weights,recurrentWeights,bias);

% View the size and dimensions of dlY.

size(dlY)
dlY.dims

% View the size of hiddenState and cellState.

size(hiddenState)
size(cellState)

% Check that the output hiddenState is the same as the last time step of output dlY.

if extractdata(dlY(:,:,end)) == hiddenState
   disp("The hidden state and the last time step are equal.");
else 
   disp("The hidden state and the last time step are not equal.")
end

% You can use the hidden state and cell state to keep track of the state of the LSTM operation and input further sequential data.
% Copyright 2019 The MathWorks, Inc.