The dimensions keep changing so the for loop is failing. How to address this?
Afficher commentaires plus anciens
Hi everyone
I am coming across a problem that appears quite circular. It complains about dimensinos. I fix it. Then it works for one loop and not the next. I am new to MATLAB so I am sure I am making a rookie mistake. How can I fix it?
Code:
layerSet = 'bilstm';
numForecasts = 10;
sTrain = CIV.V;
mu = mean(sTrain);
sigma = std(sTrain);
sTrainNorm = (sTrain-mu)/sigma;
xTrain = sTrainNorm(1:end-1);
yTrain = sTrainNorm(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,TargetUsage="trainNetwork");
options = trainingOptions('adam', ...
'MaxEpochs',300, ...
'ExecutionEnvironment','gpu', ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Shuffle','every-epoch', ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(xTrain.',yTrain.',layers,options);
yPred(1) = predict(net,(CIV.V(end)-mu)/sigma);
for t = 2:numForecasts
sTrain = [sTrain.' yPred(t-1)];
mu = mean(sTrain);
sigma = std(sTrain);
sTrainNorm = (sTrain-mu)/sigma;
xTrain = sTrainNorm(1:end-1);
yTrain = sTrainNorm(2:end);
net = trainNetwork(xTrain, yTrain ,layers,options);
yPred(t) = predict(net,yPred(t-1));
end
yPred = sigma*yPred+mu;
Error:
>> clear
>> Forecast
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Forecast (line 101)
sTrain = [sTrain yPred(t-1)];
^^^^^^
>> clear
>> Forecast
Error using trainNetwork (line 191)
The training sequences are of feature dimension 3251 but the input layer expects sequences of feature dimension 1.
Error in Forecast (line 107)
net = trainNetwork(xTrain.', yTrain.' ,layers,options);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> clear
>> Forecast
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Forecast (line 101)
sTrain = [sTrain.' yPred(t-1)];
^^^^^^^^
>>
Variable:

Thank you
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Vibration Analysis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!