Clear Filters
Clear Filters

Error using trainNetwork (line 184) The training sequences are of feature dimension 1 792 but the input layer expects sequences of feature dimension 1.

52 views (last 30 days)
% Parameters
n_samples = 1000;
signal_frequency = 10;
time = linspace(0, 1, n_samples)';
I_signal = cos(2 * pi * signal_frequency * time);
Q_signal = sin(2 * pi * signal_frequency * time);
% Create pairs of I and Q signals
X = I_signal(1:end-1); % I signal as input
y = Q_signal(2:end); % Corresponding Q signal as target output
% Define sequence length (number of time steps in each sequence)
sequence_length = 10; % You can adjust this as needed
% Prepare sequences of input and target data
X_sequences = buffer(X, sequence_length, sequence_length-1, 'nodelay').';
y_sequences = buffer(y, sequence_length, sequence_length-1, 'nodelay').';
% Split the dataset into training and testing sets
rng(0); % Set random seed for reproducibility
split_ratio = 0.8;
split_idx = round(split_ratio * size(X_sequences, 1));
X_train = X_sequences(1:split_idx, :);
y_train = y_sequences(1:split_idx, :);
X_test = X_sequences(split_idx+1:end, :);
y_test = y_sequences(split_idx+1:end, :);
% Reshape data for LSTM input (numObservations, numTimeSteps, numFeatures)
X_train = reshape(X_train, [1, size(X_train, 1), sequence_length]);
y_train = reshape(y_train, [1, size(y_train, 1), sequence_length]);
X_test = reshape(X_test, [1, size(X_test, 1), sequence_length]);
y_test = reshape(y_test, [1, size(y_test, 1), sequence_length]);
% Create an LSTM-based neural network
layers = [
sequenceInputLayer(1) % Sequence input layer for single scalar input
lstmLayer(64, 'OutputMode', 'sequence')
lstmLayer(32, 'OutputMode', 'sequence')
fullyConnectedLayer(1)
regressionLayer % Regression layer
];
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'MiniBatchSize', 32, ...
'ValidationData', {X_test, y_test}, ...
'Plots', 'training-progress', ...
'InitialLearnRate', 0.01);
% Train the LSTM network
net = trainNetwork(X_train, y_train, layers, options);
% Use the trained model to estimate Q signals from new I signals
new_I_signals = cos(2 * pi * signal_frequency * time); % Replace with your new I signals
new_X_sequences = buffer(new_I_signals, sequence_length, sequence_length-1, 'nodelay').';
new_X_sequences = reshape(new_X_sequences, [1, size(new_X_sequences, 1), sequence_length]);
estimated_Q_signals = predict(net, new_X_sequences);
% Plot the original Q signals and estimated Q signals
figure;
plot(time(2:end), Q_signal(2:end), '--', 'DisplayName', 'Original Q Signal');
hold on;
plot(time(2:end), estimated_Q_signals(2:end), 'DisplayName', 'Estimated Q Signal');
xlabel('Time');
ylabel('Amplitude');
legend('Location', 'best');
title('Q Signal Estimation with LSTM');
grid on;
I am using this code for regression problem. But I am getting Error again and again. Any help or what should I do?
Error using trainNetwork (line 184)
The training sequences are of feature dimension 1 792 but the input layer expects sequences of
feature dimension 1.
Error in Aws_LSTM_PhaseMeasure (line 51)
net = trainNetwork(X_train, y_train, layers, options);

Answers (1)

Matt J
Matt J on 26 Sep 2023
fn=@(z) reshape( num2cell(permute(z,[1,3,2]),2) ,[],1);
X_train=fn(X_train);
y_train=fn(y_train);
X_test=fn(X_test);
y_test=fn(y_test);
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'MiniBatchSize', 32, ...
'ValidationData', {X_test, y_test}, ...
'Plots', 'training-progress', ...
'InitialLearnRate', 0.01);
% Train the LSTM network
net = trainNetwork(X_train, y_train, layers, options);
  13 Comments
Matt J
Matt J on 26 Sep 2023
Actually, size of estimated_Q_signals and Q-signal should be same ... in my case size of Q-signal is 1000x1 double.
Well, that can't happen because new_X_sequences is not that size,
K>> whos new_X_sequences
Name Size Bytes Class Attributes
new_X_sequences 1x991x10 79280 double
Leostar90
Leostar90 on 27 Sep 2023
I think I chose wrong way to solve my problem ..... seems like I should solve with
"Sequence-to-One Regression Using Deep Learning". I have to predict one signal from other signal ....and for training I have synthetic data x signal and y signal (targets) .... I am new to MATLAB Deep Learning so I think for training we should pass sequences to model.
After training when I should pass one signal as input to PREDICT function and it should result in same diemension of y-signal (target) in my problem Q-signal 1000x1 double.So I think "Sequence to one regression using deep learning in MATLAB could work here.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!