Error using trainNetwork: Predictors must be a N-by-1 cell array of sequences
Hello MATLAB friends,
I’m working on a project involving time series prediction using a Gated Recurrent Unit (GRU) model. The goal is to predict whether a stock will be a ‘winning’ or ‘losing’ stock based on historical data.
However, I’m encountering an error when trying to train the network using the trainNetwork function. The error message is:
Error using trainNetwork
Invalid training data. Predictors must be a N-by-1 cell array of sequences, where N is the number of sequences. All sequences must have the same feature dimension and at least one time step.
Here’s the relevant portion of my code:
% Initialize cell arrays to store training and testing features and labels
training_features = {};
training_labels = {};
testing_features = {};
testing_labels = {};
% Process each CSV file in the combined_stocks table
for f = 1:height(combined_stocks)
% Load data from CSV file using readtable
data = readtable(fullfile(directory_path, combined_stocks.Stock(f)), ‘VariableNamingRule’, ‘preserve’);
% Selecting the required features
features = data{:, {‘open’, ‘high’, ‘low’, ‘close’, ‘MA’, ‘MA_1’}};
% Normalizing the close prices
normalized_close = (data.close – min(data.close)) / (max(data.close) – min(data.close));
% Calculating logarithmic close prices
log_close = log(data.close);
% Combine all features into a matrix
features = [features, normalized_close, log_close];
% Determine the label for the current stock
if combined_stocks.Maximum_Profit(f) > 0
labels = ones(height(data), 1); % Winning class
else
labels = zeros(height(data), 1); % Losing class
end
% Convert labels to categorical
labels = categorical(labels);
% Generate a random permutation of indices
indices = randperm(size(features, 1));
% Split the data into 70% training and 30% testing
split_point = round(size(features, 1) * 0.7);
training_indices = indices(1:split_point);
testing_indices = indices(split_point+1:end);
% Append the training and testing features and labels to the respective arrays
training_features = [training_features; {features(training_indices, :)}];
training_labels = [training_labels; {labels(training_indices)}];
testing_features = [testing_features; {features(testing_indices, :)}];
testing_labels = [testing_labels; {labels(testing_indices)}];
end
% Define the GRU network architecture
layers = [ …
sequenceInputLayer(size(training_features{1}, 2))
gruLayer(100,’OutputMode’,’last’)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
% Define the training options
options = trainingOptions(‘adam’, …
‘MaxEpochs’,100, …
‘MiniBatchSize’, 150, …
‘InitialLearnRate’, 0.01, …
‘GradientThreshold’, 1, …
‘ExecutionEnvironment’,’auto’,…
‘plots’,’training-progress’, …
‘Verbose’,false);
% Train the GRU network
net = trainNetwork(training_features, training_labels, layers, options);
In my code, training_features and training_labels are N-by-1 cell arrays. Each cell in training_features contains a matrix of size M-by-8, where M is the number of time steps and 8 is the number of features. Each cell in training_labels contains a categorical vector of length M, where M is the same as in the corresponding cell of training_features.
Despite this, I’m still getting the error. Any help would be greatly appreciated!Hello MATLAB friends,
I’m working on a project involving time series prediction using a Gated Recurrent Unit (GRU) model. The goal is to predict whether a stock will be a ‘winning’ or ‘losing’ stock based on historical data.
However, I’m encountering an error when trying to train the network using the trainNetwork function. The error message is:
Error using trainNetwork
Invalid training data. Predictors must be a N-by-1 cell array of sequences, where N is the number of sequences. All sequences must have the same feature dimension and at least one time step.
Here’s the relevant portion of my code:
% Initialize cell arrays to store training and testing features and labels
training_features = {};
training_labels = {};
testing_features = {};
testing_labels = {};
% Process each CSV file in the combined_stocks table
for f = 1:height(combined_stocks)
% Load data from CSV file using readtable
data = readtable(fullfile(directory_path, combined_stocks.Stock(f)), ‘VariableNamingRule’, ‘preserve’);
% Selecting the required features
features = data{:, {‘open’, ‘high’, ‘low’, ‘close’, ‘MA’, ‘MA_1’}};
% Normalizing the close prices
normalized_close = (data.close – min(data.close)) / (max(data.close) – min(data.close));
% Calculating logarithmic close prices
log_close = log(data.close);
% Combine all features into a matrix
features = [features, normalized_close, log_close];
% Determine the label for the current stock
if combined_stocks.Maximum_Profit(f) > 0
labels = ones(height(data), 1); % Winning class
else
labels = zeros(height(data), 1); % Losing class
end
% Convert labels to categorical
labels = categorical(labels);
% Generate a random permutation of indices
indices = randperm(size(features, 1));
% Split the data into 70% training and 30% testing
split_point = round(size(features, 1) * 0.7);
training_indices = indices(1:split_point);
testing_indices = indices(split_point+1:end);
% Append the training and testing features and labels to the respective arrays
training_features = [training_features; {features(training_indices, :)}];
training_labels = [training_labels; {labels(training_indices)}];
testing_features = [testing_features; {features(testing_indices, :)}];
testing_labels = [testing_labels; {labels(testing_indices)}];
end
% Define the GRU network architecture
layers = [ …
sequenceInputLayer(size(training_features{1}, 2))
gruLayer(100,’OutputMode’,’last’)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
% Define the training options
options = trainingOptions(‘adam’, …
‘MaxEpochs’,100, …
‘MiniBatchSize’, 150, …
‘InitialLearnRate’, 0.01, …
‘GradientThreshold’, 1, …
‘ExecutionEnvironment’,’auto’,…
‘plots’,’training-progress’, …
‘Verbose’,false);
% Train the GRU network
net = trainNetwork(training_features, training_labels, layers, options);
In my code, training_features and training_labels are N-by-1 cell arrays. Each cell in training_features contains a matrix of size M-by-8, where M is the number of time steps and 8 is the number of features. Each cell in training_labels contains a categorical vector of length M, where M is the same as in the corresponding cell of training_features.
Despite this, I’m still getting the error. Any help would be greatly appreciated! Hello MATLAB friends,
I’m working on a project involving time series prediction using a Gated Recurrent Unit (GRU) model. The goal is to predict whether a stock will be a ‘winning’ or ‘losing’ stock based on historical data.
However, I’m encountering an error when trying to train the network using the trainNetwork function. The error message is:
Error using trainNetwork
Invalid training data. Predictors must be a N-by-1 cell array of sequences, where N is the number of sequences. All sequences must have the same feature dimension and at least one time step.
Here’s the relevant portion of my code:
% Initialize cell arrays to store training and testing features and labels
training_features = {};
training_labels = {};
testing_features = {};
testing_labels = {};
% Process each CSV file in the combined_stocks table
for f = 1:height(combined_stocks)
% Load data from CSV file using readtable
data = readtable(fullfile(directory_path, combined_stocks.Stock(f)), ‘VariableNamingRule’, ‘preserve’);
% Selecting the required features
features = data{:, {‘open’, ‘high’, ‘low’, ‘close’, ‘MA’, ‘MA_1’}};
% Normalizing the close prices
normalized_close = (data.close – min(data.close)) / (max(data.close) – min(data.close));
% Calculating logarithmic close prices
log_close = log(data.close);
% Combine all features into a matrix
features = [features, normalized_close, log_close];
% Determine the label for the current stock
if combined_stocks.Maximum_Profit(f) > 0
labels = ones(height(data), 1); % Winning class
else
labels = zeros(height(data), 1); % Losing class
end
% Convert labels to categorical
labels = categorical(labels);
% Generate a random permutation of indices
indices = randperm(size(features, 1));
% Split the data into 70% training and 30% testing
split_point = round(size(features, 1) * 0.7);
training_indices = indices(1:split_point);
testing_indices = indices(split_point+1:end);
% Append the training and testing features and labels to the respective arrays
training_features = [training_features; {features(training_indices, :)}];
training_labels = [training_labels; {labels(training_indices)}];
testing_features = [testing_features; {features(testing_indices, :)}];
testing_labels = [testing_labels; {labels(testing_indices)}];
end
% Define the GRU network architecture
layers = [ …
sequenceInputLayer(size(training_features{1}, 2))
gruLayer(100,’OutputMode’,’last’)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
% Define the training options
options = trainingOptions(‘adam’, …
‘MaxEpochs’,100, …
‘MiniBatchSize’, 150, …
‘InitialLearnRate’, 0.01, …
‘GradientThreshold’, 1, …
‘ExecutionEnvironment’,’auto’,…
‘plots’,’training-progress’, …
‘Verbose’,false);
% Train the GRU network
net = trainNetwork(training_features, training_labels, layers, options);
In my code, training_features and training_labels are N-by-1 cell arrays. Each cell in training_features contains a matrix of size M-by-8, where M is the number of time steps and 8 is the number of features. Each cell in training_labels contains a categorical vector of length M, where M is the same as in the corresponding cell of training_features.
Despite this, I’m still getting the error. Any help would be greatly appreciated! matlab, deep learning toolbox, gru, trainnetwork MATLAB Answers — New Questions