%% Sequence Classification by Sweeping Hyperparameters
% This template function defines the training data, network architecture, and 
% training options for a sequence classification experiment. Experiment Manager 
% uses the outputs of this function to call the <matlab:doc('trainNetwork') |trainNetwork|> 
% function. For more information on sequence classification, see <matlab:helpview('deeplearning','ClassifySequenceDataUsingLSTMNetworksExample') 
% Sequence Classification Using Deep Learning>. For more information on built-in 
% experiments, see <matlab:helpview('deeplearning','exp-mgr-create-experiment') 
% Configure Built-In Training Experiment>.
% 
% Modify this function as a template to set up your own experiment.
%% Input
%% 
% * |params| is a structure with fields from the Experiment Manager hyperparameter 
% table.
%% Output
%% 
% * |Xtrain| is a cell array containing sequences of varying length. 
% * |Ttrain| is a categorical vector of labels 1 to 9.
% * |layers| is a layer graph that defines the neural network architecture.
% * |options| is a |trainingOptions| object.
function [XTrain,TTrain,layers,options] ={functionName}(params)
%% Load Training Data
% Load the training data as in-memory cell arrays by using the |japaneseVowelsTrainData| 
% function. The entries in |XTrain| are matrices with 12 rows (one row for each 
% feature) and varying length (one column for each time step). Get the sequence 
% lengths for each observation and sort the data by sequence length.
[XTrain,TTrain] = japaneseVowelsTrainData;
numObservations = numel(XTrain);
for i=1:numObservations
    sequence = XTrain{i};
    sequenceLengths(i) = size(sequence,2);
end
[~,idx] = sort(sequenceLengths);
XTrain = XTrain(idx);
TTrain = TTrain(idx);
%% 
% Replace the data with your own training and validation data. For more information, 
% see <matlab:helpview('deeplearning','data-sets-for-deep-learning') Data Sets 
% for Deep Learning>.
%% Define Network Architecture
% Define the convolutional neural network architecture in the layer graph |layers|. 
inputSize = 12;
numHiddenUnits = 100;
numClasses = 9;
layers = [ ...
    sequenceInputLayer(inputSize)
    bilstmLayer(numHiddenUnits,'OutputMode','last')
    fullyConnectedLayer(numClasses)
    softmaxLayer
    classificationLayer];
%% 
% Replace the layer graph with your own layer graph to train. For more information, 
% see <matlab:helpview('deeplearning','list_of_layers') List of Deep Learning 
% Layers>.
%% Specify Training Options
% Call the <matlab:doc('trainingOptions') |trainingOptions|> function to specify 
% the training options for the experiment. Read the hyperparameters specified 
% in Experiment Manager by accessing the fields of the input |params|. For example, 
% access the initial learning rate |myInitialLearnRate| defined in the hyperparameter 
% table with the argument value |params.myInitialLearnRate|.
options = trainingOptions('adam', ...
    'ExecutionEnvironment','cpu', ...
    'GradientThreshold',1, ...
    'MaxEpochs',100, ...
    'MiniBatchSize',20, ...
    'SequenceLength','longest', ...
    'InitialLearnRate', params.myInitialLearnRate, ...
    'Shuffle','never', ...
    'Verbose',0);
%% 
% End of experiment setup function.
end
%% 
% Copyright 2020-2021 The MathWorks, Inc.