MATLAB Answers

Input sequence for the LSTM layer

1 view (last 30 days)
Saif Aljanahi
Saif Aljanahi on 9 May 2021
Edited: Saif Aljanahi on 9 May 2021
I'm struggling in preparing the input data for the LSTM layer, I have an accelerometer dataset that composes activities like (walking and fall...etc).
the raw file is (6...x 4), 3 axes and the last column is the activity label.
I know that the input for the LSTM layer should be sequence data, I have 18 classes (walking, fall, running..etc).
When I run my code I get this error:
Error using train network (line 184)
Invalid training data. The output size (18) of the last layer does not match the number of classes (17).
but my data is having 18 classes, the samples of the accelerometer readings are this:
I know there's something wrong with the way I've prepared my data and I need your help, thanks in advance.
till now I've written this code:
Scaling the Data:
%%%scaling.m
%% Find min and max
trainData =readtable('C:\Users\saifm\Downloads\Documents\ECE\Graduation_Project\SisFall\SisFall_dataset\SISFALL\FULL_DATA\TRAIN_SISFALL.csv');
testData =readtable('C:\Users\saifm\Downloads\Documents\ECE\Graduation_Project\SisFall\SisFall_dataset\SISFALL\FULL_DATA\TEST_SISFALL.csv');
if isempty(gcp('nocreate'))
parpool('local')
end
actLabels ={'D01','D02','D03','D04','D05','D06','D07','D08','D09','D10','F01','F02','F03','F04','F05',...
'F06','F07','F08'};
data = [trainData; testData];
ax = (data.ACCX)';
ay = (data.ACCY)';
az = (data.ACCZ)';
ax = ax(:)';
ay = ay(:)';
az = az(:)';
[~, psax] = mapminmax(ax);
[~, psay] = mapminmax(ay);
[~, psaz] = mapminmax(az);
%% Scale data (max value -> +1, min value -> -1)
NA = size(trainData, 1);
parfor k = 1:NA
trainData(k, :).ACCX = mapminmax('apply', trainData(k, :).ACCX, psax);
trainData(k, :).ACCY = mapminmax('apply', trainData(k, :).ACCY, psay);
trainData(k, :).ACCZ = mapminmax('apply', trainData(k, :).ACCZ, psaz);
end
NB = size(testData, 1);
parfor k = 1:NB
testData(k, :).ACCX = mapminmax('apply', testData(k, :).ACCX, psax);
testData(k, :).ACCY = mapminmax('apply', testData(k, :).ACCY, psay);
testData(k, :).ACCZ = mapminmax('apply', testData(k, :).ACCZ, psaz);
end
clearvars -except trainData testData actLabels
save 'SisDataset'
LSTM Network:
%%%LSTM.m
NA = size(trainData, 1);
XA = cell(NA, 1);
parfor k = 1:NA
XA{k} = [trainData{k, 'ACCX'}; trainData{k, 'ACCY'}; trainData{k, 'ACCZ'}];
end
actTrain = categorical(trainData.ACTIVITY);
inputSize = 3;
numHiddenUnits = 100;
numClasses = 18;
%Define the layers
layers = [ ...
sequenceInputLayer(inputSize)
bilstmLayer(numHiddenUnits,'OutputMode','last')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
%% Define training options
maxEpochs = 70;
miniBatchSize = 28;
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'Plots','training-progress');
%% Train LSTM
[net, info] = trainNetwork(XA, actTrain, layers, options);
%DATA CONVERSION FOR THE TEST DATA
NB = size(testData, 1);
XB = cell(NB, 1);
parfor k = 1:NB
XB{k} = [testData{k, 'ACCX'}; testData{k, 'ACCY'}; testData{k, 'ACCZ'}];
end
actTest = categorical(testData.ACTIVITY);
%% Check performance
acc = classify(net, XB);
displayResult(actTest, acc, actLabels)

Answers (0)

Community Treasure Hunt

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

Start Hunting!