how to plot accuracy?
Error using trainNetwork (line 150)
Invalid training data. Sequence responses must have the same sequence length as the corresponding predictors.
Error in Untitled (line 92)
net = trainNetwork(x_train_seq, y_train_seq, layers, options);
% Load the files
file_path_e = ‘sig.xlsx’;
file_path_sig = ‘E.xlsx’;
% Read the files
data_e = readtable(file_path_e);
data_sig = readtable(file_path_sig);
% Prepare data
x = table2array(data_e);
y = table2array(data_sig);
% Ensure x and y have the same length
min_length = min(length(x), length(y));
x = x(1:min_length);
y = y(1:min_length);
% Display initial data types
disp(‘Initial data types:’);
disp([‘x type: ‘, class(x)]);
disp([‘y type: ‘, class(y)]);
% Convert to numeric arrays if not already
x = str2double(x);
y = str2double(y);
% Display number of NaNs before removing them
fprintf(‘Number of NaNs in x before removal: %dn’, sum(isnan(x)));
fprintf(‘Number of NaNs in y before removal: %dn’, sum(isnan(y)));
% Handle non-numeric entries by removing NaNs
valid_indices = ~isnan(x) & ~isnan(y);
x = x(valid_indices);
y = y(valid_indices);
% Display number of valid data points after removal
fprintf(‘Number of valid data points after preprocessing: %dn’, length(x));
% Ensure x and y have the same length again after removing NaNs
min_length = min(length(x), length(y));
x = x(1:min_length);
y = y(1:min_length);
% Check if there are enough valid entries
if min_length <= 1
error(‘Not enough valid data points after preprocessing.’);
end
% Reshape data to be compatible with LSTM input (samples, timesteps, features)
x = reshape(x, [], 1);
y = reshape(y, [], 1);
% Scale data using min-max normalization
x_scaled = (x – min(x)) / (max(x) – min(x));
y_scaled = (y – min(y)) / (max(y) – min(y));
% Split data into train and test sets
cv = cvpartition(length(x_scaled), ‘HoldOut’, 0.2);
x_train = x_scaled(training(cv));
y_train = y_scaled(training(cv));
x_test = x_scaled(test(cv));
y_test = y_scaled(test(cv));
% Create sequences for LSTM
seq_length = 10;
[x_train_seq, y_train_seq] = create_sequences(x_train, y_train, seq_length);
[x_test_seq, y_test_seq] = create_sequences(x_test, y_test, seq_length);
% Reshape for LSTM
x_train_seq = reshape(x_train_seq, [size(x_train_seq, 1), seq_length, 1]);
x_test_seq = reshape(x_test_seq, [size(x_test_seq, 1), seq_length, 1]);
% Build the LSTM model
layers = [
sequenceInputLayer(1)
lstmLayer(20, ‘OutputMode’, ‘sequence’)
dropoutLayer(0.2)
lstmLayer(20)
dropoutLayer(0.2)
fullyConnectedLayer(1)
regressionLayer];
options = trainingOptions(‘adam’, …
‘MaxEpochs’, 300, …
‘MiniBatchSize’, 20, …
‘InitialLearnRate’, 0.001, …
‘ValidationData’, {x_test_seq, y_test_seq}, …
‘Plots’, ‘training-progress’, …
‘Verbose’, 0);
% Train the model
net = trainNetwork(x_train_seq, y_train_seq, layers, options);
% Plot loss curve
training_info = net.TrainingHistory;
figure;
plot(training_info.TrainingLoss, ‘DisplayName’, ‘Train’);
hold on;
plot(training_info.ValidationLoss, ‘DisplayName’, ‘Validation’);
title(‘Model loss’);
xlabel(‘Epoch’);
ylabel(‘Loss’);
legend(‘show’);
hold off;
% Function to create sequences
function [xs, ys] = create_sequences(x_data, y_data, seq_length)
xs = [];
ys = [];
for i = 1:(length(x_data) – seq_length)
x_seq = x_data(i:i+seq_length-1);
y_seq = y_data(i+seq_length-1); % Adjust index to ensure same length
xs = [xs; x_seq’];
ys = [ys; y_seq’];
end
endError using trainNetwork (line 150)
Invalid training data. Sequence responses must have the same sequence length as the corresponding predictors.
Error in Untitled (line 92)
net = trainNetwork(x_train_seq, y_train_seq, layers, options);
% Load the files
file_path_e = ‘sig.xlsx’;
file_path_sig = ‘E.xlsx’;
% Read the files
data_e = readtable(file_path_e);
data_sig = readtable(file_path_sig);
% Prepare data
x = table2array(data_e);
y = table2array(data_sig);
% Ensure x and y have the same length
min_length = min(length(x), length(y));
x = x(1:min_length);
y = y(1:min_length);
% Display initial data types
disp(‘Initial data types:’);
disp([‘x type: ‘, class(x)]);
disp([‘y type: ‘, class(y)]);
% Convert to numeric arrays if not already
x = str2double(x);
y = str2double(y);
% Display number of NaNs before removing them
fprintf(‘Number of NaNs in x before removal: %dn’, sum(isnan(x)));
fprintf(‘Number of NaNs in y before removal: %dn’, sum(isnan(y)));
% Handle non-numeric entries by removing NaNs
valid_indices = ~isnan(x) & ~isnan(y);
x = x(valid_indices);
y = y(valid_indices);
% Display number of valid data points after removal
fprintf(‘Number of valid data points after preprocessing: %dn’, length(x));
% Ensure x and y have the same length again after removing NaNs
min_length = min(length(x), length(y));
x = x(1:min_length);
y = y(1:min_length);
% Check if there are enough valid entries
if min_length <= 1
error(‘Not enough valid data points after preprocessing.’);
end
% Reshape data to be compatible with LSTM input (samples, timesteps, features)
x = reshape(x, [], 1);
y = reshape(y, [], 1);
% Scale data using min-max normalization
x_scaled = (x – min(x)) / (max(x) – min(x));
y_scaled = (y – min(y)) / (max(y) – min(y));
% Split data into train and test sets
cv = cvpartition(length(x_scaled), ‘HoldOut’, 0.2);
x_train = x_scaled(training(cv));
y_train = y_scaled(training(cv));
x_test = x_scaled(test(cv));
y_test = y_scaled(test(cv));
% Create sequences for LSTM
seq_length = 10;
[x_train_seq, y_train_seq] = create_sequences(x_train, y_train, seq_length);
[x_test_seq, y_test_seq] = create_sequences(x_test, y_test, seq_length);
% Reshape for LSTM
x_train_seq = reshape(x_train_seq, [size(x_train_seq, 1), seq_length, 1]);
x_test_seq = reshape(x_test_seq, [size(x_test_seq, 1), seq_length, 1]);
% Build the LSTM model
layers = [
sequenceInputLayer(1)
lstmLayer(20, ‘OutputMode’, ‘sequence’)
dropoutLayer(0.2)
lstmLayer(20)
dropoutLayer(0.2)
fullyConnectedLayer(1)
regressionLayer];
options = trainingOptions(‘adam’, …
‘MaxEpochs’, 300, …
‘MiniBatchSize’, 20, …
‘InitialLearnRate’, 0.001, …
‘ValidationData’, {x_test_seq, y_test_seq}, …
‘Plots’, ‘training-progress’, …
‘Verbose’, 0);
% Train the model
net = trainNetwork(x_train_seq, y_train_seq, layers, options);
% Plot loss curve
training_info = net.TrainingHistory;
figure;
plot(training_info.TrainingLoss, ‘DisplayName’, ‘Train’);
hold on;
plot(training_info.ValidationLoss, ‘DisplayName’, ‘Validation’);
title(‘Model loss’);
xlabel(‘Epoch’);
ylabel(‘Loss’);
legend(‘show’);
hold off;
% Function to create sequences
function [xs, ys] = create_sequences(x_data, y_data, seq_length)
xs = [];
ys = [];
for i = 1:(length(x_data) – seq_length)
x_seq = x_data(i:i+seq_length-1);
y_seq = y_data(i+seq_length-1); % Adjust index to ensure same length
xs = [xs; x_seq’];
ys = [ys; y_seq’];
end
end Error using trainNetwork (line 150)
Invalid training data. Sequence responses must have the same sequence length as the corresponding predictors.
Error in Untitled (line 92)
net = trainNetwork(x_train_seq, y_train_seq, layers, options);
% Load the files
file_path_e = ‘sig.xlsx’;
file_path_sig = ‘E.xlsx’;
% Read the files
data_e = readtable(file_path_e);
data_sig = readtable(file_path_sig);
% Prepare data
x = table2array(data_e);
y = table2array(data_sig);
% Ensure x and y have the same length
min_length = min(length(x), length(y));
x = x(1:min_length);
y = y(1:min_length);
% Display initial data types
disp(‘Initial data types:’);
disp([‘x type: ‘, class(x)]);
disp([‘y type: ‘, class(y)]);
% Convert to numeric arrays if not already
x = str2double(x);
y = str2double(y);
% Display number of NaNs before removing them
fprintf(‘Number of NaNs in x before removal: %dn’, sum(isnan(x)));
fprintf(‘Number of NaNs in y before removal: %dn’, sum(isnan(y)));
% Handle non-numeric entries by removing NaNs
valid_indices = ~isnan(x) & ~isnan(y);
x = x(valid_indices);
y = y(valid_indices);
% Display number of valid data points after removal
fprintf(‘Number of valid data points after preprocessing: %dn’, length(x));
% Ensure x and y have the same length again after removing NaNs
min_length = min(length(x), length(y));
x = x(1:min_length);
y = y(1:min_length);
% Check if there are enough valid entries
if min_length <= 1
error(‘Not enough valid data points after preprocessing.’);
end
% Reshape data to be compatible with LSTM input (samples, timesteps, features)
x = reshape(x, [], 1);
y = reshape(y, [], 1);
% Scale data using min-max normalization
x_scaled = (x – min(x)) / (max(x) – min(x));
y_scaled = (y – min(y)) / (max(y) – min(y));
% Split data into train and test sets
cv = cvpartition(length(x_scaled), ‘HoldOut’, 0.2);
x_train = x_scaled(training(cv));
y_train = y_scaled(training(cv));
x_test = x_scaled(test(cv));
y_test = y_scaled(test(cv));
% Create sequences for LSTM
seq_length = 10;
[x_train_seq, y_train_seq] = create_sequences(x_train, y_train, seq_length);
[x_test_seq, y_test_seq] = create_sequences(x_test, y_test, seq_length);
% Reshape for LSTM
x_train_seq = reshape(x_train_seq, [size(x_train_seq, 1), seq_length, 1]);
x_test_seq = reshape(x_test_seq, [size(x_test_seq, 1), seq_length, 1]);
% Build the LSTM model
layers = [
sequenceInputLayer(1)
lstmLayer(20, ‘OutputMode’, ‘sequence’)
dropoutLayer(0.2)
lstmLayer(20)
dropoutLayer(0.2)
fullyConnectedLayer(1)
regressionLayer];
options = trainingOptions(‘adam’, …
‘MaxEpochs’, 300, …
‘MiniBatchSize’, 20, …
‘InitialLearnRate’, 0.001, …
‘ValidationData’, {x_test_seq, y_test_seq}, …
‘Plots’, ‘training-progress’, …
‘Verbose’, 0);
% Train the model
net = trainNetwork(x_train_seq, y_train_seq, layers, options);
% Plot loss curve
training_info = net.TrainingHistory;
figure;
plot(training_info.TrainingLoss, ‘DisplayName’, ‘Train’);
hold on;
plot(training_info.ValidationLoss, ‘DisplayName’, ‘Validation’);
title(‘Model loss’);
xlabel(‘Epoch’);
ylabel(‘Loss’);
legend(‘show’);
hold off;
% Function to create sequences
function [xs, ys] = create_sequences(x_data, y_data, seq_length)
xs = [];
ys = [];
for i = 1:(length(x_data) – seq_length)
x_seq = x_data(i:i+seq_length-1);
y_seq = y_data(i+seq_length-1); % Adjust index to ensure same length
xs = [xs; x_seq’];
ys = [ys; y_seq’];
end
end accuracy, lstm MATLAB Answers — New Questions