Invalid training data. Sequence responses must have the same sequence length as the corresponding predictors.

6 views (last 30 days)
Confused as to why categorical did nto work is there somwhere else that I need to fix
clc; clear all; close all;
load Projectdata.mat
% Split Data Glucose
GlucoseReadings_T = GlucoseReadings';
GlucoseReadings_train = GlucoseReadings_T;
train_GlucoseReadings = GlucoseReadings_train(1:84,:);
train_GR_output = GR_output(1:17);
% Data Batch Glucose
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1749,84]));
val_GlucoseReadings = GlucoseReadings_train(85:102,:);
val_GR_output = GR_output(85:102);
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1749,18]));
test_GlucoseReadings =GlucoseReadings_train(103:120,:);
test_GR_output = GR_output(103:120);
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1749,18]));
numFeatures = size(GlucoseReadings_T,2);
% number of hidden units represent the size of the data
numHiddenUnits = 120;
%number of classes repsresnt healthy and diabetic
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
whos
Name Size Bytes Class Attributes GR_output 1x120 7296 string GlucoseReadings 1749x120 1679040 double GlucoseReadingsTest 1749x18 251856 double GlucoseReadingsTrain 1749x84 1175328 double GlucoseReadingsVal 1749x18 251856 double GlucoseReadings_T 120x1749 1679040 double GlucoseReadings_train 120x1749 1679040 double INS_output 1x120 7296 string InsulinReadings 1758x120 1687680 double layers 7x1 3332 nnet.cnn.layer.Layer numClasses 1x1 8 double numFeatures 1x1 8 double numHiddenUnits 1x1 8 double options 1x1 253569 nnet.cnn.TrainingOptionsADAM test_GR_output 1x18 1248 string test_GlucoseReadings 18x1749 251856 double train_GR_output 1x17 1048 string train_GlucoseReadings 84x1749 1175328 double val_GR_output 1x18 1248 string val_GlucoseReadings 18x1749 251856 double
net = trainNetwork(GlucoseReadingsTrain,categorical(train_GR_output),layers,options);
Error using trainNetwork (line 184)
Invalid training data. Sequence responses must have the same sequence length as the corresponding predictors.
% Test
miniBatchSize = 27;
GR_outputPred = classify(net,GlucoseReadingsTest,...
'MiniBatchSize',miniBatchSize,...
'Environment','cpu');
acc = mean(GR_outputPred(:) == categorical(test_GR_output(:)))
figure
t = confusionchart(categorical(test_GR_output(:)),GR_outputPred(:));
figure
t1 = confusionchart(categorical(test_INS_output(:)),INS_outputPred(:));

Accepted Answer

Walter Roberson
Walter Roberson on 4 Mar 2022
net = trainNetwork(GlucoseReadingsTrain,categorical(train_GR_output),layers,options);
Your GlucoseReadingsTrain is 1749 x 84
Your train_GR_output is 1 x 18
There isn't anything remotely compatible about those.
You should not be training on a single data set. You should be creating a dataset that has samples from all of your classes, and the corresponding section parameter should indicate which class each sample belongs to.
As I advised you before, you should be creating a variable to hold the number of training samples for each class, and the number of test samples, and you should be creating subsets using code similar to
class1_training = class1_data(1:number_class1_train,:);
class1_test = class1_data(number_class1_train+1:number_class1_train+number_class1_test,:);
class1_valid = class1_data(number_class1_train+number_class1_test+1:end,:);
response1_training = class1_response(1:number_class1_train,:);
response1_test = class1_response(number_class1_train+1:number_class1_train+number_class1_test,:);
response1_valid = class1_response(number_class1_train+number_class1_test+1:end,:);
%more stuff
training_data = [class1_training; class2_training];
response_data = categorical([response1_training; response2_training]);

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