USING LSTM TO CLASSIFY DATA
Please see my code below
% Step 1: Load the data from the Excel file
data = readmatrix(‘LSTMdataIn.xlsx’);
% Step 2: Create labels
labels = [ones(200, 1); 2*ones(200, 1); 3*ones(200, 1); 4*ones(200, 1); 5*ones(200, 1)];
% Step 3: Reshape the data
numTimeSteps = 100;
numFeatures = 1;
reshapedData = reshape(data’, numFeatures, numTimeSteps, []);
% Step 4: Split the data into training and testing sets
cv = cvpartition(labels, ‘HoldOut’, 0.2);
trainIdx = training(cv);
testIdx = test(cv);
XTrain = reshapedData(:, :, trainIdx);
YTrain = labels(trainIdx);
XTest = reshapedData(:, :, testIdx);
YTest = labels(testIdx);
% Step 5: Create and train the LSTM network
numHiddenUnits = 100;
layers = [ …
sequenceInputLayer(100)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(5)
softmaxLayer
classificationLayer];
options = trainingOptions(‘adam’, ‘MaxEpochs’, 10, ‘MiniBatchSize’, 32);
net = trainNetwork(XTrain, categorical(YTrain), layers, options);
% Step 6: Evaluate the trained network
YTestPred = classify(net, XTest);
accuracy = sum(YTestPred == categorical(YTest)) / numel(YTest);
I get the following error
Error using trainNetwork (line 191)
The training sequences are of feature dimension 1 100 but the input layer expects sequences of feature dimension 100.Please see my code below
% Step 1: Load the data from the Excel file
data = readmatrix(‘LSTMdataIn.xlsx’);
% Step 2: Create labels
labels = [ones(200, 1); 2*ones(200, 1); 3*ones(200, 1); 4*ones(200, 1); 5*ones(200, 1)];
% Step 3: Reshape the data
numTimeSteps = 100;
numFeatures = 1;
reshapedData = reshape(data’, numFeatures, numTimeSteps, []);
% Step 4: Split the data into training and testing sets
cv = cvpartition(labels, ‘HoldOut’, 0.2);
trainIdx = training(cv);
testIdx = test(cv);
XTrain = reshapedData(:, :, trainIdx);
YTrain = labels(trainIdx);
XTest = reshapedData(:, :, testIdx);
YTest = labels(testIdx);
% Step 5: Create and train the LSTM network
numHiddenUnits = 100;
layers = [ …
sequenceInputLayer(100)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(5)
softmaxLayer
classificationLayer];
options = trainingOptions(‘adam’, ‘MaxEpochs’, 10, ‘MiniBatchSize’, 32);
net = trainNetwork(XTrain, categorical(YTrain), layers, options);
% Step 6: Evaluate the trained network
YTestPred = classify(net, XTest);
accuracy = sum(YTestPred == categorical(YTest)) / numel(YTest);
I get the following error
Error using trainNetwork (line 191)
The training sequences are of feature dimension 1 100 but the input layer expects sequences of feature dimension 100. Please see my code below
% Step 1: Load the data from the Excel file
data = readmatrix(‘LSTMdataIn.xlsx’);
% Step 2: Create labels
labels = [ones(200, 1); 2*ones(200, 1); 3*ones(200, 1); 4*ones(200, 1); 5*ones(200, 1)];
% Step 3: Reshape the data
numTimeSteps = 100;
numFeatures = 1;
reshapedData = reshape(data’, numFeatures, numTimeSteps, []);
% Step 4: Split the data into training and testing sets
cv = cvpartition(labels, ‘HoldOut’, 0.2);
trainIdx = training(cv);
testIdx = test(cv);
XTrain = reshapedData(:, :, trainIdx);
YTrain = labels(trainIdx);
XTest = reshapedData(:, :, testIdx);
YTest = labels(testIdx);
% Step 5: Create and train the LSTM network
numHiddenUnits = 100;
layers = [ …
sequenceInputLayer(100)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(5)
softmaxLayer
classificationLayer];
options = trainingOptions(‘adam’, ‘MaxEpochs’, 10, ‘MiniBatchSize’, 32);
net = trainNetwork(XTrain, categorical(YTrain), layers, options);
% Step 6: Evaluate the trained network
YTestPred = classify(net, XTest);
accuracy = sum(YTestPred == categorical(YTest)) / numel(YTest);
I get the following error
Error using trainNetwork (line 191)
The training sequences are of feature dimension 1 100 but the input layer expects sequences of feature dimension 100. lstm MATLAB Answers — New Questions