As per my understanding, you are training a neural network for a regression task and encountering an error when using the “trainNetwork” function.
I was able to reproduce the error at my end and identified the issue. There was a mismatch between the expected input dimensions of the network and the actual dimensions of the data being fed into it.
I made the following changes in the code to ensure smooth data processing and handle dimension mismatching:
- The X, XTest and Y matrices are arranged in format (features, timesteps, samples) which is the expected input format for the LSTM layer.
- The “permute” function is removed from the “trainNetwork” function arguments as it was unnecessary.
- The data was reshaped to fit the expected input format for “trainNetwork” function.
% Manually defined dimensions
B = 3; % Number of operating points (samples)
T = 50; % Number of time steps
C = 2; % Number of input features
O = 1; % Number of output features
%%%%%Changes here to match the expected input format%%%%%
% Example data for different operating points (OPs)
X = zeros(C, T, B);
Y = zeros(O, T, B);
% Operating point 1
X(1, :, 1) = 2000;
X(2, :, 1) = 70;
Y(1, :, 1) = rand(1, T) * 100 + 50;
% Operating point 2
X(1, :, 2) = 4000;
X(2, :, 2) = 50;
Y(1, :, 2) = rand(1, T) * 100 + 40;
% Operating point 3
X(1, :, 3) = 3000;
X(2, :, 3) = 60;
Y(1, :, 3) = rand(1, T) * 100 + 45;
% Network parameters
numHiddenUnits = 50; % Number of LSTM cells
% Define network architecture
layers = [ ...
sequenceInputLayer(C) % C input features
lstmLayer(numHiddenUnits, 'OutputMode', 'sequence') % LSTM layer
fullyConnectedLayer(O) % O output features
regressionLayer]; % Regression layer for continuous outputs
% Set training options
options = trainingOptions('adam', ...
'MaxEpochs', 100, ...
'MiniBatchSize', 1, ...
'GradientThreshold', 1, ...
'InitialLearnRate', 0.01, ...
'Verbose', false, ...
'Plots', 'training-progress');
% Reshape data to fit the expected input format for trainNetwork
% Each sequence is of size B, with cell of size C x T
XCell = squeeze(mat2cell(X, C, T, ones(1, B)));
YCell = squeeze(mat2cell(Y, O, T, ones(1, B)));
% Train network
net = trainNetwork(XCell, YCell, layers, options);
%%%%%Changes here to match the expected output format%%%%%
% Example inference for a new operating point
XTest = zeros(C, T, 1); % Test data in the form C x T x 1
XTest(1, :, 1) = 3500;
XTest(2, :, 1) = 65;
% Predict curve
YPred = predict(net, squeeze(XTest));
% Display predicted temperature curve
plot(squeeze(YPred)); % Squeeze to obtain the 2D curve for plotting
xlabel('Time Steps');
ylabel('Predicted Temperature');
title('Predicted Temperature Curve for New Operating Point');
Refer to the following MathWorks documentation on “trainNetwork” for more information:
Hope this is beneficial!