Problem with a LSTM network
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/272403/image.png)
Please let me know where is the error and what should I do to train the network correctly.
Thanks in advance.
0 Kommentare
Antworten (2)
Vimal Rathod
am 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 Kommentare
Vimal Rathod
am 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.
NGR MNFD
am 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Image Data Workflows finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!