Understanding sliding window for input

13 views (last 30 days)
Bob Matthews
Bob Matthews on 15 Mar 2020
Edited: Bob Matthews on 15 Mar 2020
Hello
I am new to deep learning LSTM neural networks
I wish to adapt the following code example with these changes
1) I have two files to begin with - an Input file with x records - each record has a time stamp field and 3 forex crosses (minute closing prices)
and an Output file with x - 99 records - each record has a time stamp field and a 'prediction trend' field
2) I wish on each training iteration to input 100 records from the Input file
The 'prediction trend' field in the Output predicts the trend for the next 100 minutes
Please help me understand how to make these changes to the code
Thank you
Bob M
University of Otago
Dunedin, New Zealand
-------------------------------------------------------
data = chickenpox_dataset;
data = [data{:}];
figure
plot(data)
xlabel("Month")
ylabel("Cases")
title("Monthly Cases of Chickenpox")
% Partition the training and test data. Train on the first 90% of the sequence and test on the last 10%.
numTimeStepsTrain = floor(0.9*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
% Standardize Data
% For a better fit and to prevent the training from diverging, standardize the training data to have zero mean and unit variance. At prediction time, you must standardize the test data using the same parameters as the training data.
mu = mean(dataTrain);
sig = std(dataTrain);
dataTrainStandardized = (dataTrain - mu) / sig;
% Prepare Predictors and Responses
% To forecast the values of future time steps of a sequence, specify the responses to be the training sequences with values shifted by one time step. That is, at each time step of the input sequence, the LSTM network learns to predict the value of the next time step. The predictors are the training sequences without the final time step.
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
% Define LSTM Network Architecture
% Create an LSTM regression network. Specify the LSTM layer to have 200 hidden units.
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
% Specify the training options. Set the solver to 'adam' and train for 250 epochs. To prevent the gradients from exploding, set the gradient threshold to 1. Specify the initial learn rate 0.005, and drop the learn rate after 125 epochs by multiplying by a factor of 0.2.
options = trainingOptions('adam', ...
'MaxEpochs',250, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
% Train LSTM Network
% Train the LSTM network with the specified training options by using trainNetwork.
net = trainNetwork(XTrain,YTrain,layers,options);

Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!