I was trying to load my input array to LSTM for data training and validation. I encountered an error stating that the expected input are different from my input dimension:
Error using trainNetwork
The training sequences are of feature dimension 1677 1000 but the input layer expects sequences of feature dimension 1000 2.
Error in noise_kernel_deconvolve (line 203)
net = trainNetwork(X_train, Y_train, layers, options);
the size of X
and X_train
are both 3D array with dimension 2097 1000 2
and 1677 1000 2
as required by the input size of the LSTM, and the size of Y
and Y_train
are 2097 1
and 1677 1
respectively, 2D array also required by trainNetwork()
for LSTM.
I don't have a clear idea how the input data get dimensionally split under the hood of these stacking layers, so it is a bit hard for me to diagnose where the problem might happen.
My layer is structured as:
layers = [
%sequenceInputLayer(num_features, 'MinLength', sequence_length)
%sequenceInputLayer([sequence_length, num_features])
sequenceInputLayer([sequence_length, 2])
convolution1dLayer(3, 64, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling1dLayer(2, 'Stride', 2)
convolution1dLayer(3, 128, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling1dLayer(2, 'Stride', 2)
convolution1dLayer(3, 256, 'Padding', 'same')
batchNormalizationLayer
reluLayer
flattenLayer % Add this layer to flatten the output before LSTM
lstmLayer(100, 'OutputMode', 'last')
fullyConnectedLayer(64)
reluLayer
dropoutLayer(0.5)
fullyConnectedLayer(2) % 2 output neurons for binary classification
softmaxLayer
classificationLayer
];
and the reshaping data for X and Y code is :
% Adjust sequence_length based on your data
sequence_length = 1000; % Ensure this is compatible with your data
num_features = 2; % We're working with 1D time series data
% Check the number of samples
num_samples = floor(length(final_filtered_signal) / sequence_length);
% Reshape the data to have the correct dimensions: [samples, time steps, features]
%X_init = reshape(final_filtered_signal(1:num_samples * sequence_length), [num_samples, sequence_length, num_features]);
X_init = reshape(final_filtered_signal(1:num_samples * sequence_length), [num_samples, sequence_length, 1]);
% Duplicate the data to match the new number of features
X_init = repmat(X_init, 1, 1, num_features); % Now X_init is [num_samples, sequence_length, num_features]
% Initialize X with ones, now 2 features
X = ones(2097, 1000, num_features);
for i = 1:2097
X(i, :, :) = X_init(i, :, :); % Ensure the third dimension is populated
end