Index in position 1 exceeds array bounds. Index must not exceed 24. Error in RNN_CW2 (line 20) GlucoseReadings_T = GlucoseReadings_T(ind, :);

1 view (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
rand('seed', 0)
GlucoseReadings_T = GlucoseReadings';
GR_outputC1 = categorical(GR_output);
CS = categories(GR_outputC1);
train_index = []; val_index = []; test_index = [];
for i = 1 : length(CS)
indi = find(GR_outputC1==CS{i});
% Shuffling data
indi = indi(randperm(length(indi)));
% 2/3---train, 1/6---val, 1/6---test
index1 = round(length(indi)*2/3);
index2 = round(length(indi)*(2/3+1/6));
train_index = [train_index indi(1:index1)];
val_index = [val_index indi(1+index1:index2)];
test_index = [test_index indi(1+index2:end)];
end
ind = [train_index val_index test_index];
GlucoseReadings_T = GlucoseReadings_T(ind, :);
Index in position 1 exceeds array bounds. Index must not exceed 24.
GR_output = categorical(GR_output(ind));
% Split Data
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(18:21,:);
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(:));

Accepted Answer

yanqi liu
yanqi liu on 23 Feb 2022
clc; clear all; close all;
load GlucoseReadings.mat
rand('seed', 0)
GlucoseReadings_T = GlucoseReadings';
GR_outputC1 = categorical(GR_output);
len = min(length(GR_outputC1),size(GlucoseReadings_T,1));
GlucoseReadings_T = GlucoseReadings_T(1:len,:);
GR_outputC1 = GR_outputC1(1:len);
CS = categories(GR_outputC1);
train_index = []; val_index = []; test_index = [];
for i = 1 : length(CS)
indi = find(GR_outputC1==CS{i});
% Shuffling data
indi = indi(randperm(length(indi)));
% 2/3---train, 1/6---val, 1/6---test
index1 = round(length(indi)*2/3);
index2 = round(length(indi)*(2/3+1/6));
train_index = [train_index indi(1:index1)];
val_index = [val_index indi(1+index1:index2)];
test_index = [test_index indi(1+index2:end)];
end
ind = [train_index val_index test_index];
GlucoseReadings_T = GlucoseReadings_T(ind, :);
GR_output = categorical(GR_output(ind));
% Split Data
GlucoseReadings_train = GlucoseReadings_T;
train_GlucoseReadings = GlucoseReadings_train(1:17,:);
train_GR_output = GR_output(1:17);
% Data Batch
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1438,size(train_GlucoseReadings,1)]));
val_GlucoseReadings = GlucoseReadings_train(18:21,:);
val_GR_output = GR_output(18:21);
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1438,size(val_GlucoseReadings,1)]));
test_GlucoseReadings =GlucoseReadings_train(22:24,:);
test_GR_output = GR_output(22:24);
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1438,size(test_GlucoseReadings,1)]));
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