Error in training Neural Network: Invalid training data. For a recurrent layer with output mode 'last', responses must be a categorical column vector.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to train an LSTM on a time-series dataset.
The training data is of dimensions (time x samples) and in preprocessing I put it into a cell (samples x 1) with each array in the cell having dimension (1 x time)
The target data is of dimensions (1 x samples) and specifies the category the sample belongs to. In preprocessing I create a cell (samples x 1) of categorical labels with dimension (1 x categories). The labels are arrays with one-hot encoding to indicate the category.
I'm getting the error: "Invalid training data. For a recurrent layer with output mode 'last', responses must be a categorical column vector."
I'm not sure what is causing this error, any help would be greatly appreciated! Thank you!
Code:
%training
train_on = full(getfield(train_data,'spikes'));
targets = mod(getfield(train_data,'target_direction'), 360);
tm = getfield(train_data,'target_motion');
directions = unique(targets);
directions_index = 1:length(directions);
%% preprocess data
true = zeros(length(directions), length(targets));
for i = 1:length(targets)
ind = find(directions == targets(i));
true(ind, i) = 1;
end
train_on = train_on(tm(1): end-1, :);
train_on = permute(train_on, [2, 1]);
true = permute(true, [2, 1]);
input = cell(size(train_on, 1), 1);
labels = cell(size(train_on, 1), 1);
for i = 1:size(train_on,1)
input{i, 1} = [train_on(i, :)];
labels{i, 1} = categorical(true(i,:));
end
%% make LSTM
numFeatures = 1; numHiddenUnits = 100; numClasses = length(directions);
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits,'OutputMode','last')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
maxEpochs = 10; miniBatchSize = 20;
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(input,labels,layers,options);
1 comentario
shan liu
el 23 de Mayo de 2022
I got a same error as you and resolved it.
at your code there:
labels = cell(size(train_on, 1), 1);
for i = 1:size(train_on,1)
input{i, 1} = [train_on(i, :)];
labels{i, 1} = categorical(true(i,:));
end
Check your labels. It is a cell, not a categorical. Make it to size(train_on,1)*1 categorical.
Respuestas (1)
Harsha Priya Daggubati
el 11 de Mayo de 2020
Hi,
As it is mentioned in the error message, responses (i.e. training data labels) should be of categorical type. In your case can you check the type of labels, just to ensure it is of categorical type with size samplesx1.
Also, are you sure that each sample has information about only one feature?
0 comentarios
Ver también
Categorías
Más información sobre Deep Learning Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!