Error using Deep Learning model LSTM

8 views (last 30 days)
Manny
Manny on 9 Dec 2024
Edited: Joss Knight on 11 Dec 2024
Hi eveyrone
I am getting error when I run my code. I am new to MATLAB so I am not sure how to fix this issue. I got the code from this book and I copied it as exactly as it is presented but only changed the how the data was created. The book made up data but I used actual stock data. Other than this, the codes are the same:
Can someone help me fix this issue?
Error:
Error using trainNetwork (line 191)
The training sequences are of feature dimension 2122 but the input layer expects sequences of
feature dimension 1.
Error in ForecastVolatilityLSTM (line 92)
net = trainNetwork(xTrain,yTrain,layers,options);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Code:
% LSTM
layerSet = 'two lstm';
n = length(CIV.COMPOSITE_IMPLIED_VOLATILITY);
nTrain = floor(0.8*n);
sTrain = CIV.COMPOSITE_IMPLIED_VOLATILITY(1:nTrain);
sTest = CIV.COMPOSITE_IMPLIED_VOLATILITY(nTrain+1:n);
sVal = sTest;
mu = mean(sTrain);
sigma = std(sTrain);
sTrainNorm = (sTrain-mu)/sigma;
sTestNorm = (sTest-mu)/sigma;
sTest = sTestNorm(1:end-1);
xTrain = sTrainNorm(1:end-1);
yTrain = sTrainNorm(2:end);
muVal = mean(sVal);
sigmaVal = std(sVal);
sValNorm = (sVal-muVal)/sigmaVal;
xVal = sValNorm(1:end-1);
yVal = sValNorm(2:end);
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
switch layerSet
case 'lstm'
layers = [sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
case 'bilstm'
layers = [sequenceInputLayer(numFeatures)
bilstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
case 'two lstm'
layers = [sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
reluLayer
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
otherwise
error('Only 3 sets of layers are available');
end
analyzeNetwork(layers);
options = trainingOptions('adam', ...
'MaxEpochs',300, ...
'ExecutionEnvironment','gpu', ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Shuffle','every-epoch', ...
'ValidationData',{xVal, yVal}, ...
'ValidationFrequency',5, ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(xTrain,yTrain,layers,options);
Variables:
CIV table:
Thank you

Answers (2)

Walter Roberson
Walter Roberson on 9 Dec 2024
Numeric or Cell Array
Vector sequences
c-by-s matrices, where c is the number of features of the sequences and s is the sequence length.
Your xtrain is 2122 x 1, so that represents c = 2122 the number of features in the sequence, and 1 is the sequence length.
But you specified
numFeatures = 1;
%...
layers = [sequenceInputLayer(numFeatures)
so your code is expecting a sequence with 1 feature.
If you really want 1 feature, then you need to call
net = trainNetwork(xTrain.',yTrain.',layers,options);
  1 Comment
Manny
Manny on 9 Dec 2024
Thank you @Walter Roberson. That fixed the error. I am now getting a new one in the network window. What does this error mean? How do I fix it?

Sign in to comment.


Joss Knight
Joss Knight on 9 Dec 2024
Moved: Walter Roberson on 10 Dec 2024
This error is because you are analyzing your network as a dlnetwork, which does not support output layers.
It depends on how you opened the Analyzer app. If you opened it from Deep Network Designer, then to analyze a DAGNetwork you need to use the -v1 option to deepNetworkDesigner to get the legacy behaviour:
If you opened the App by calling analyzeNetwork, you need to add the option TargetUsage="trainNetwork" to get the legacy behaviour.
trainNetwork and DAGNetwork are no longer recommended, so ideally you should update your code to using trainnet and dlnetwork.
  10 Comments
Manny
Manny on 10 Dec 2024
I am not an experienced MATLAB user. I got the code above from a book I mentioned earlier and I just copied it but changed the data source. If MATLAB is recommending to not use the functions I did use then I need help in moving over the code. Can you please re-write the old code using the new functions? I hope this isn't too much to ask.
Thank you
Joss Knight
Joss Knight on 11 Dec 2024
Edited: Joss Knight on 11 Dec 2024
I understand. I'm saying it is too much to ask - for me. You're going to have to read documentation, read the code you're adapting, learn what it's doing, and make some straightforward changes as I listed. But maybe someone else will help more directly.
Alternatively, just continue to use DAGNetwork and trainNetwork with the workarounds I gave you (-v1 option to deepNetworkDesigner, TargetUsage="trainNetwork" option to analyzeNetwork). Or downgrade MATLAB to R2023b or earlier.

Sign in to comment.

Categories

Find more on Parallel and Cloud in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!