Invalid training data. For classification tasks, responses must be a vector of categorical responses. For regression tasks, responses must be a vector, a matrix, or a 4-D arra

3 views (last 30 days)
Nathaniel Porter
Nathaniel Porter on 22 Feb 2022
Answered: yanqi liu on 23 Feb 2022
clc; clear all; close all;
load GlucoseReadings.mat
% Split Data
GlucoseReadings_T = GlucoseReadings';
GlucoseReadings_train = GlucoseReadings_T;
train_GlucoseReadings = GlucoseReadings_train(1:17,:);
train_GR_output = GR_output(1:17);
% Data Batch
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1438,17]));
val_GlucoseReadings = GlucoseReadings_train(18:21,:);
val_GR_output = GR_output(18:21);
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1438,4]));
test_GlucoseReadings =GlucoseReadings_train(22:24,:);
test_GR_output = GR_output(22:24);
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1438,3]));
numFeatures = size(GlucoseReadings_T,2);
% number of hidden units represent the size of the data
numHiddenUnits = 24;
%number of classes represent different patients normal,LIS,type2....
numClasses = length(categories(categorical(GR_output)));
layers = [ ...
sequenceInputLayer(numFeatures)
%dropoutLayer(0.5)
instanceNormalizationLayer
bilstmLayer(round(numHiddenUnits/2),'OutputMode','sequence')
fullyConnectedLayer(numClasses)
instanceNormalizationLayer
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',100, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'ValidationData',{GlucoseReadingsVal, val_GR_output},...
'LearnRateDropFactor',0.2,...
'LearnRateDropPeriod',5,...
'Plots','training-progress');
% Train
net = trainNetwork(GlucoseReadingsTrain,train_GR_output,layers,options);
Error using trainNetwork (line 184)
Invalid training data. For classification tasks, responses must be a vector of categorical responses. For regression tasks, responses must be a vector, a matrix, or a 4-D array of numeric responses which must not contain NaNs.
% Test
miniBatchSize = 27;
GR_outputPred = classify(net,GlucoseReadingsTest, ...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment', 'cpu');
acc = mean(GR_outputPred(:) == categorical(test_GR_output(:)))
figure
t = confusionchart(categorical(test_GR_output(:)),GR_outputPred(:));

Accepted Answer

yanqi liu
yanqi liu on 23 Feb 2022
clc; clear all; close all;
load GlucoseReadings.mat
GR_output = categorical(GR_output);
% Split Data
GlucoseReadings_T = GlucoseReadings';
GlucoseReadings_train = GlucoseReadings_T;
train_GlucoseReadings = GlucoseReadings_train(1:17,:);
train_GR_output = GR_output(1:17);
% Data Batch
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1438,17]));
val_GlucoseReadings = GlucoseReadings_train(18:21,:);
val_GR_output = GR_output(18:21);
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1438,4]));
test_GlucoseReadings =GlucoseReadings_train(22:24,:);
test_GR_output = GR_output(22:24);
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1438,3]));
numFeatures = size(GlucoseReadings_T,2);
% number of hidden units represent the size of the data
numHiddenUnits = 24;
%number of classes represent different patients normal,LIS,type2....
numClasses = length(categories(categorical(GR_output)));
layers = [ ...
sequenceInputLayer(numFeatures)
%dropoutLayer(0.5)
instanceNormalizationLayer
bilstmLayer(round(numHiddenUnits/2),'OutputMode','sequence')
fullyConnectedLayer(numClasses)
instanceNormalizationLayer
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',100, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'ValidationData',{GlucoseReadingsVal, val_GR_output},...
'LearnRateDropFactor',0.2,...
'LearnRateDropPeriod',5,...
'Plots','training-progress');
% Train
net = trainNetwork(GlucoseReadingsTrain,train_GR_output,layers,options);
% Test
miniBatchSize = 27;
GR_outputPred = classify(net,GlucoseReadingsTest, ...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment', 'cpu');
acc = mean(GR_outputPred(:) == categorical(test_GR_output(:)))
figure
t = confusionchart(categorical(test_GR_output(:)),GR_outputPred(:));

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by