matlab The version is 2018b And above .
%% % Loading sequence data % data description : in total 270 The training samples were divided into two groups 9 class , The number of training samples in each group is different , Each training sample consists of 12 It is composed of two eigenvectors ,
[XTrain,YTrain] = japaneseVowelsTrainData; % Data visualization figure plot(XTrain{1}')
xlabel("Time Step") title("Training Observation 1") legend("Feature " +
string(1:12),'Location','northeastoutside') %% %LSTM After grouping, the same amount of training samples can be trained , So as to improve the training efficiency
% If the number of samples in each group is different , Small batch splitting , It is necessary to ensure the same number of training samples % First, find the number of samples in each group and the total number of groups numObservations =
numel(XTrain); for i=1:numObservations sequence = XTrain{i}; sequenceLengths(i)
= size(sequence,2); end % The number of data in each group before and after drawing figure subplot(1,2,1)
bar(sequenceLengths) ylim([0 30]) xlabel("Sequence") ylabel("Length")
title("Sorted Data") % Sort test data by sequence length [sequenceLengths,idx] =
sort(sequenceLengths); XTrain = XTrain(idx); YTrain = YTrain(idx);
subplot(1,2,2) bar(sequenceLengths) ylim([0 30]) xlabel("Sequence")
ylabel("Length") title("Sorted Data") %% % set up LSTM The number of small batch groups of training data miniBatchSize =
27; %% % definition LSTM Network architecture : % Specifies the input size as the sequence size 12( Dimensions of input data ) % Specifies that the 100 Two way search of hidden units LSTM
layer , And output the last element of the sequence . % Specify nine classes , Contains the size of 9 Fully connected layer of , Heel softmax Layer and classification layer . inputSize = 12;
numHiddenUnits = 100; numClasses = 9; layers = [ ...
sequenceInputLayer(inputSize) bilstmLayer(numHiddenUnits,'OutputMode','last')
fullyConnectedLayer(numClasses) softmaxLayer classificationLayer] %% % Specify training options :
% The solver is 'adam' % The gradient threshold is 1, The maximum number of rounds is 100. % 27 As a small batch number . % Fill the data so that the length is the same as the longest sequence , The sequence length is specified as
'longest'. % The data remains sorted by the length of the sequence , Don't scramble the data . % 'ExecutionEnvironment' Designated as
'cpu', Set to 'auto' Indicate use GPU. maxEpochs = 100; miniBatchSize = 27; options =
trainingOptions('adam', ... 'ExecutionEnvironment','cpu', ...
'GradientThreshold',1, ... 'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ... 'SequenceLength','longest', ...
'Shuffle','never', ... 'Verbose',0, ... 'Plots','training-progress'); %%
% train LSTM network net = trainNetwork(XTrain,YTrain,layers,options); %% % test LSTM network % Load test set
[XTest,YTest] = japaneseVowelsTestData;
% because LSTM Small batches of similar length have been grouped 27, The test needs to sort the data in the same way . numObservationsTest =
numel(XTest); for i=1:numObservationsTest sequence = XTest{i};
sequenceLengthsTest(i) = size(sequence,2); end [sequenceLengthsTest,idx] =
sort(sequenceLengthsTest); XTest = XTest(idx); YTest = YTest(idx);
% use classify Classify , Specify small batch size 27, The data in the specified group is filled with the longest data miniBatchSize = 27; YPred =
classify(net,XTest, ... 'MiniBatchSize',miniBatchSize, ...
'SequenceLength','longest'); % Calculation of classification accuracy acc = sum(YPred == YTest)./numel(YTest)
 

Technology