The dimensions keep changing so the for loop is failing. How to address this?

25 次查看(过去 30 天)
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

采纳的回答

Umar
Umar 2024-12-11,1:01

Hi @Manny,

When working with LSTM networks in MATLAB, especially for time series forecasting, it is crucial to ensure that the dimensions of the input data are consistent throughout the training and prediction processes. The errors you are experiencing can be attributed to a few common pitfalls. Let me break down the issues and provide solutions step-by-step.

Concatenation Error: The error message Dimensions of arrays being concatenated are not consistent indicates that the dimensions of sTrain and yPred(t-1) do not match when you attempt to concatenate them. This typically occurs when the dimensions of the arrays differ in either the number of rows or columns.

Training Network Error: The error about training sequences are of feature dimension 3251 but the input layer expects sequences of feature dimension 1 suggests that the input data being fed into the network does not match the expected input size defined in the sequenceInputLayer. This mismatch can occur if the data is not reshaped correctly.

So, to resolve these issues, follow these steps:

Step 1: Ensure Consistent Dimensions

When concatenating sTrain and yPred(t-1), ensure that both arrays have the same number of rows. Since sTrain is a column vector, you should ensure that yPred(t-1) is also a column vector. You can achieve this by using the following code:

sTrain = [sTrain; yPred(t-1)]; % Use semicolon to concatenate vertically

Step 2: Reshape Data for Training

Before training the network, ensure that xTrain and yTrain are reshaped correctly. Since you are using a sequence input layer expecting a feature dimension of 1, you should reshape your training data accordingly:

   %   Reshape to [1, sequenceLength, numFeatures]
   xTrain = reshape(sTrainNorm(1:end-1), [1, numel(sTrainNorm(1:end-1)), 1]); 
   % Reshape similarly
  yTrain = reshape(sTrainNorm(2:end), [1, numel(sTrainNorm(2:end)), 1]); 

Step 3: Update the Prediction Loop

In your prediction loop, ensure that the new sTrain is updated correctly after each prediction. The updated code should look like this:

for t = 2:numForecasts
  sTrain = [sTrain; yPred(t-1)]; % Ensure vertical concatenation
  mu = mean(sTrain);
  sigma = std(sTrain);
  sTrainNorm = (sTrain - mu) / sigma;
    % Reshape for training
    xTrain = reshape(sTrainNorm(1:end-1), [1, numel(sTrainNorm(1:end-1)), 1]);
    yTrain = reshape(sTrainNorm(2:end), [1, numel(sTrainNorm(2:end)), 1]);
    net = trainNetwork(xTrain, yTrain, layers, options);
    yPred(t) = predict(net, reshape(yPred(t-1), [1, 1, 1])); % Reshape for prediction
  end

Here is the revised section of your code with the suggested changes:

for t = 2:numForecasts
  sTrain = [sTrain; yPred(t-1)]; % Concatenate vertically
  mu = mean(sTrain);
  sigma = std(sTrain);
  sTrainNorm = (sTrain - mu) / sigma;
    % Reshape for training
    xTrain = reshape(sTrainNorm(1:end-1), [1, numel(sTrainNorm(1:end-1)), 1]);
    yTrain = reshape(sTrainNorm(2:end), [1, numel(sTrainNorm(2:end)), 1]);
    net = trainNetwork(xTrain, yTrain, layers, options);
    yPred(t) = predict(net, reshape(yPred(t-1), [1, 1, 1])); % Reshape for prediction
  end

By ensuring that the dimensions of your arrays are consistent and reshaping your data appropriately, you should be able to resolve the errors you are encountering. MATLAB's LSTM implementation requires careful attention to the shape of the data, especially when dealing with sequences. If you continue to face issues, consider using debugging techniques such as checking the size of your variables at each step to identify where the mismatch occurs.

Hope this helps.

  2 个评论
Manny
Manny 2024-12-11,2:06
Thank you so much @Umar! The code ran with no issues. I have another question. How can I output the RMSE for each iteration?
Umar
Umar 2024-12-11,11:23

Hi @Manny,

It is my pleasure to help you out. Regarding your follow-up question about outputting the Root Mean Square Error (RMSE) for each iteration, you can calculate RMSE after each prediction as follows:

rmse = zeros(1, numForecasts); % Preallocate RMSE array
for t = 2:numForecasts
  % ... (existing prediction code)
    % Calculate RMSE
    rmse(t) = sqrt(mean((yTrain - yPred(2:end)).^2)); % Compare with         actual values
  end
  disp(rmse); % Display RMSE for each iteration

This will allow you to monitor the performance of your model throughout the forecasting process.

Hope this helps!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by