Error using Deep Learning model LSTM
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (2)
Walter Roberson
el 9 de Dic. de 2024
https://www.mathworks.com/help/deeplearning/ref/trainnetwork.html#mw_36a68d96-8505-4b8d-b338-44e1efa9cc5e defines for the sequences input:
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);
Joss Knight
el 9 de Dic. de 2024
Movida: Walter Roberson
el 10 de Dic. de 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 comentarios
Joss Knight
el 11 de Dic. de 2024
Editada: Joss Knight
el 11 de Dic. de 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.
Ver también
Categorías
Más información sobre Deep Learning Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!