Trying to make a RNN for 2D signal data classification to 2D classified matrix output. Error using trainNetwork (line 165) Invalid training data. Responses must be a vector of categorical responses, or a cell array of categorical response sequences.
5 views (last 30 days)
Show older comments
Hello Everyone,
I'm trying to implement a Neural Network classification algorithm for signal data with a shape (800,500) , with 800 being the number of time steps and 500 being the number of observations. I want to train a NN to identify for every timestep if it belongs to class 0, 1, or -1 . So, my responses should have the same shape with the XTrain data, (800,500).
After trying to use Xtrain and YTrain in the form of simple 2 dimensional arrays I understood that this is not possible, so I made changed their form to cell arrays as the bibliography requires.
My XTrain data have the form of a cell array of double sequences and so do the YTrain data. Out of 500 observations I used 70%, which are 350 observations for my Train data.
As the Matlab trainNetwork bibliography suggest:
But, I'm still getting the same error:
The Layers and Options for my RNN so far are the following, please make sugestions :)
Please help
0 Comments
Answers (1)
Srivardhan Gadila
on 29 Mar 2021
According to the documentation of the Input Arguments: sequences & responses of the trainNetwork function for the syntax net = trainNetwork(sequences,responses,layers,options) the input data should be of N-by-1 cell array of numeric arrays, where N is the number of observations and each observation must be a c-by-s matrix, where c is the number of features of the sequences and s is the sequence length in case of Vector sequences. Whereas the responses should be N-by-1 cell array of categorical sequences of labels, where N is the number of observations with each observation as a 1-by-s sequence of categorical labels, where s is the sequence length of the corresponding predictor sequence.
The following code may help you:
%% Create network.
inputSize = 800;
numClasses = 3;
numHiddenUnits = 100;
layers = [ ...
sequenceInputLayer(1,'Name','Sequence Input')
lstmLayer(numHiddenUnits,'Name','LSTM Layer')
fullyConnectedLayer(numClasses,'Name','FC')
softmaxLayer('Name','Softmax')
classificationLayer('Name','Classification Layer')];
lgraph = layerGraph(layers);
analyzeNetwork(lgraph)
%% Create Random Training data.
numTrainSamples = 50;
trainData = arrayfun(@(x)rand([1 inputSize]),1:numTrainSamples,'UniformOutput',false)';
trainLabels = arrayfun(@(x)categorical(randi([-1 1], 1,inputSize)),1:numTrainSamples,'UniformOutput',false)';
size(trainData)
size(trainLabels)
%% Train the network.
options = trainingOptions('adam', ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise',...
'Verbose',1, ...
'Plots','training-progress');
net = trainNetwork(trainData,trainLabels,lgraph,options);
See Also
Categories
Find more on Classification Learner App in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!