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

% 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);

답변 (1개)

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

although it worked but after training I got this error.
Error using DAGNetwork/predict (line 166)
The prediction sequences are of feature dimension 1 991 but the input layer expects sequences
of feature dimension 1.
Error in SeriesNetwork/predict (line 279)
Y = this.UnderlyingDAGNetwork.predict(X, varargin{:});
Error in Aws_LSTM_PhaseMeasure (line 64)
estimated_Q_signals = predict(net, new_X_sequences);
Put X in the same format as X_train.
K>> X_train
X_train =
792×1 cell array
{[ 1 0.9980 0.9921 0.9823 0.9685 0.9510 0.9296 0.9046 0.8761 0.8440]}
{[ 0.9980 0.9921 0.9823 0.9685 0.9510 0.9296 0.9046 0.8761 0.8440 0.8086]}
{[ 0.9921 0.9823 0.9685 0.9510 0.9296 0.9046 0.8761 0.8440 0.8086 0.7701]}
{[ 0.9823 0.9685 0.9510 0.9296 0.9046 0.8761 0.8440 0.8086 0.7701 0.7285]}
{[ 0.9685 0.9510 0.9296 0.9046 0.8761 0.8440 0.8086 0.7701 0.7285 0.6840]}
{[ 0.9510 0.9296 0.9046 0.8761 0.8440 0.8086 0.7701 0.7285 0.6840 0.6367]}
{[ 0.9296 0.9046 0.8761 0.8440 0.8086 0.7701 0.7285 0.6840 0.6367 0.5870]}
{[ 0.9046 0.8761 0.8440 0.8086 0.7701 0.7285 0.6840 0.6367 0.5870 0.5350]}
{[ 0.8761 0.8440 0.8086 0.7701 0.7285 0.6840 0.6367 0.5870 0.5350 0.4808]}
{[ 0.8440 0.8086 0.7701 0.7285 0.6840 0.6367 0.5870 0.5350 0.4808 0.4248]}
{[ 0.8086 0.7701 0.7285 0.6840 0.6367 0.5870 0.5350 0.4808 0.4248 0.3670]}
...
You mean I should make changes in here?
X = I_signal(1:end-1); % I signal as input
y = Q_signal(2:end); % Corresponding Q signal as target output
Like this
fn=@(z) reshape( num2cell(permute(z,[1,3,2]),2) ,[],1);
X=fn(X);
y=fn(y);
yes but there was an error.
Error using buffer
Signal X must be numeric, logical, or a character array.
Error in Aws_LSTM_PhaseMeasure (line 20)
X_sequences = buffer(X, sequence_length, sequence_length-1, 'nodelay').';
The reformatting fn(X) was to meet the requirements of predict(). Other commands like buffer() may require a different format.
I am trying to find format for buffer on google but failed.
I get no errors with,
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);
% 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, fn(new_X_sequences));
Problem is here actually.
Q_signal is 1000x1 double
and
estimated_Q_signals is 991x1 cell
and every cell is 1x10 row vector.
so I got this error.
Error using plot
Invalid data argument.
Error in Aws_LSTM_PhaseMeasure (line 69)
plot(time(2:end), estimated_Q_signals(2:end), 'DisplayName', 'Estimated Q Signal');
% 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, fn(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');
It's not clear why that's a problem. Did you expect 991 estimates or not? Isn't the sequence length supposed to be 10? That's what you said in your code.
% Define sequence length (number of time steps in each sequence)
sequence_length = 10; % You can adjust this as needed
Actually, size of estimated_Q_signals and Q-signal should be same ... in my case size of Q-signal is 1000x1 double.
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');
y = Q_signal(2:end); % Corresponding Q signal as target output
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
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2023년 9월 26일

댓글:

2023년 9월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by