Problem with a LSTM network

5 views (last 30 days)
Matteo Soldini
Matteo Soldini on 18 Feb 2020
Answered: NGR MNFD on 4 Jul 2021
Hi all,
there is something wrong in my script and, first of all, I would say that I'm completely new to MATLAB and NNs, so I'm sorry for all the mistakes that I made in the script.
data = readtable('sensor.csv');
% Remove sensor_15, that is full of 0s, and sensor_50, that has too much
% NaN
data(:,{'Var1','sensor_15','sensor_50'}) = [];
% Remove all the rows with BROKEN label (7 of 220313)
data(ismember(data.machine_status,'BROKEN'),:) = [];
S = data;
% Drop timestamp and machine_status to have only sensors variable
S(:,{'timestamp','machine_status'}) = [];
Sensors = table2array(S);
% Interpolate all NaN in each sensor
nanSensors = isnan(Sensors);
t = 1:numel(Sensors);
Sensors(nanSensors) = interp1(t(~nanSensors), Sensors(~nanSensors), t(nanSensors));
% Create a categorical array containing only the targets
MachineStatus = categorical(data.machine_status);
% Divide dataset in training set and test set
PD = 0.70;
SensorsTrain = Sensors(1:round(PD*length(MachineStatus)),:);
MachineStatusTrain = MachineStatus(1:round(PD*length(MachineStatus)));
SensorsTest = Sensors(round(PD*length(MachineStatus)):end,:);
MachineStatusTest = MachineStatus(round(PD*length(MachineStatus)):end);
% Transform sensors arrays in a cell array with only one cell (154219x50
% for training) because it is only one sequence of observations
SensorsTrain = num2cell(SensorsTrain,[1 2]);
SensorsTest = num2cell(SensorsTest,[1 2]);
% Transpose the cell to have the features as rows
SensorsTrain = cellfun(@transpose,SensorsTrain,'UniformOutput',false);
SensorsTest = cellfun(@transpose,SensorsTest,'UniformOutput',false);
% For loop to obtain a cell array with one cell of dimension 1x154219 for
% the responses
for i = 1:length(MachineStatusTrain)
MSTClassNum = MachineStatusTrain(i,1);
MSTClassMatrix(1,1:154219) = MSTClassNum;
MST = {MSTClassMatrix(:,:)};
end
MachineStatusTrain = MST';
% LSTM Neural Network
numFeatures = 50
numHiddenUnits = 100
numClasses = 2
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer
]
options = trainingOptions('adam', ...
'MaxEpochs',100, ...
'GradientThreshold', 1, ...
'plots','training-progress', ...
'Verbose',false);
net = trainNetwork(SensorsTrain,MachineStatusTrain,layers,options);
sensor.csv has a variable named timestamp with date and hour for each observation, 52 sensors, one in each variable and a target variable named machine_status (NORMAL, RECOVERING, BROKEN).
The neural network starts but there is only one iteration per epoch and this is the problem. I think I wrote something wrong because the training set has more than 100.000 observations and also selecting a minibatch size, it doesn't change.
Please let me know where is the error and what should I do to train the network correctly.
Thanks in advance.

Answers (2)

Vimal Rathod
Vimal Rathod on 27 Feb 2020
Hi,
The small problem with your code is that you are not putting the dimensions of the SesnorsTrain and MachineStatusTrain properly and thus creating a single cell out of the whole sensor data is making your network take the whole sequence as a batch. As your dimensions are not proper, setting the minibatch size option also doesn't work.
You could refer to the following link to know more about how to set the dimensions of your training Data.
Hope this helps!
  3 Comments
Vimal Rathod
Vimal Rathod on 28 Feb 2020
Yeah you could have that or you could also have a normal matrix of dimensions NxP, but it should be just a matrix not a matrix in a cell completely.
Matteo Soldini
Matteo Soldini on 28 Feb 2020
Ok, if I use a matrix NxP then I have to transpose it to have a sensor in each row, right?

Sign in to comment.


NGR MNFD
NGR MNFD on 4 Jul 2021
Hello . I hope you have a good day. I sent the article to your service. I implemented the coding part in the MATLAB software, but to implement my network, two lines of setlayers, training MATLAB 2014 give me an error. What other function do you think I should replace? Do you think the codes I wrote are correct?( I used gait-in-neurodegenerative-disease-database in physionet website.) Thanks a lot

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!