Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.

16 views (last 30 days)
clc; clear all; close all;
%Import/Upload data
load GlucoseReadings.mat
% change to label vector
CS = categories(categorical(GR_output));
Z1 = []; Z2 = [];
for i = 1 : length(GR_output)
Z1(i,1) = find(GR_output(i)==CS);
end
%for i = 1 : length(Y2)
%Z2(i,1) = find(Y2(i)==CS);
%end
Yo1 = GR_output;
%Yo2 = Y2;
GR_output= Z1;
%Y2 = Z2;
%transposing glucose data
GlucoseReadings_T = GlucoseReadings';
%Shuffling data to take randomly
rand('seed', 0)
ind = randperm(size(GlucoseReadings_T, 1));
GlucoseReadings_T = GlucoseReadings_T(ind, :);
GR_output = GR_output(ind);
%Separating data in training, validation and testing data
GlucoseReadings_train = GlucoseReadings_T;
%Partioning data for training 70%
train_GlucoseReadings = GlucoseReadings_train(1:17,:);
%Corresponding X(input) data to Y(output) data
train_GR_output = GR_output(1:17);
%reshaping data into 4D array
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1438,1,1,17]));
%Separating and partioning for validation data 15%
val_GlucoseReadings = GlucoseReadings_train(18:21,:);
%Corresponding X(input) data to Y(output) data
val_GR_output = GR_output(18:21);
%reshaping data into 4D array
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1438,1,1,18])); %Train data
%Separating and partioning for test data 15%
test_GlucoseReadings = GlucoseReadings_train(19:24,:);
%Corresponding X(input) data to Y(output) data
test_GR_output = GR_output(19:24);
%reshaping data into 4D array
GlucoseReadingsTest=(reshape(GlucoseReadings_X1', [1438,1,1,18])); %Train data
%% NETWORK ARCHITECTURE
layers = [imageInputLayer([1438 1 1]) % Creating the image layer
convolution2dLayer([102 1],3,'Stride',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2,'Padding',[0 0 0 1])
dropoutLayer
fullyConnectedLayer(1)
regressionLayer];
% Specify training options.
opts = trainingOptions('sgdm', ...
'MaxEpochs',1500, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',{GlucoseReadingsVal,val_GR_output},...
'LearnRateDropFactor',0.2,...
'LearnRateDropPeriod',5,...
'ExecutionEnvironment', 'cpu', ...
'ValidationPatience',Inf);
%% Train network
%net = trainNetwork(XTrain,Trainoutfinal,layers,opts);
yc = train_GR_output(:);
net1 = trainNetwork(GlucoseReadingsTrain,yc,layers,opts);
%% Compare against testing Data
Ypredicted = predict(net1, GlucoseReadingsTest)
predictionError = test_GR_output - GR_outputpredicted;
squares = predictionError.^2;
rmse = sqrt(mean(squares))
figure
scatter(GR_outputpredicted, test_GR_output,'+')
title ('True value vs Predicted Value')
xlabel ("Predicted Value")
ylabel ("True Value")
hold on
plot([-3 3], [-7 7], 'b--')
  4 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Feb 2022
fileurl = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/902755/GlucoseReadings.mat';
%Import/Upload data
tname = tempname() + ".mat";
urlwrite(fileurl, tname);
data = load(tname);
GR_output = data.GR_output;
GlucoseReadings = data.GlucoseReadings;
% change to label vector
CS = categories(categorical(GR_output));
Z1 = []; Z2 = [];
for i = 1 : length(GR_output)
Z1(i,1) = find(GR_output(i)==CS);
end
%for i = 1 : length(Y2)
%Z2(i,1) = find(Y2(i)==CS);
%end
Yo1 = GR_output;
%Yo2 = Y2;
GR_output= Z1;
%Y2 = Z2;
%transposing glucose data
GlucoseReadings_T = GlucoseReadings';
%Shuffling data to take randomly
rand('seed', 0)
ind = randperm(size(GlucoseReadings_T, 1));
GlucoseReadings_T = GlucoseReadings_T(ind, :);
GR_output = GR_output(ind);
%Separating data in training, validation and testing data
GlucoseReadings_train = GlucoseReadings_T;
%Partioning data for training 70%
train_GlucoseReadings = GlucoseReadings_train(1:17,:);
%Corresponding X(input) data to Y(output) data
train_GR_output = GR_output(1:17);
%reshaping data into 4D array
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1438,1,1,17]));
%Separating and partioning for validation data 15%
val_GlucoseReadings = GlucoseReadings_train(18:21,:);
That is obviously 4 by something
%Corresponding X(input) data to Y(output) data
val_GR_output = GR_output(18:21);
%reshaping data into 4D array
whos
Name Size Bytes Class Attributes CS 2x1 238 cell GR_output 24x1 192 double GlucoseReadings 1438x24 276096 double GlucoseReadingsTrain 1438x1x1x17 195568 double GlucoseReadings_T 24x1438 276096 double GlucoseReadings_train 24x1438 276096 double Yo1 1x26 1518 string Z1 26x1 208 double Z2 0x0 0 double ans 1x47 94 char data 1x1 277950 struct fileurl 1x89 178 char i 1x1 8 double ind 1x24 192 double tname 1x1 308 string train_GR_output 17x1 136 double train_GlucoseReadings 17x1438 195568 double val_GR_output 4x1 32 double val_GlucoseReadings 4x1438 46016 double
and checking we see that Yes, it is 4 x something
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1438,1,1,18])); %Train data
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
The code is treating it as if it is 18 x something, not 4 x something.
But why 18 anyhow? You pulled off 17 columns to make GclusoseReadingsTrain, and you pulled off 4 columns to make val_GlucoseReadings, and columns 22 to 24 are not extracted from yet... but why pull out 18 ? It would make more sense if you were doing 18:end to pull out everything other than the first 17.
%Separating and partioning for test data 15%
test_GlucoseReadings = GlucoseReadings_train(19:24,:);
Why are you re-using 19, 20, 21, as well as adding in 22, 23, 24, but at the same time ignoring 18 ??
I would recommend to you that you use variables to hold the number you are selecting for training, and then be consistent about using indices like 1:numtrain, numtrain+1:numtrain+numtest, numtrain+numtest+1:end
%Corresponding X(input) data to Y(output) data
test_GR_output = GR_output(19:24);
%reshaping data into 4D array
GlucoseReadingsTest=(reshape(GlucoseReadings_X1', [1438,1,1,18])); %Train data
%% NETWORK ARCHITECTURE
layers = [imageInputLayer([1438 1 1]) % Creating the image layer
convolution2dLayer([102 1],3,'Stride',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2,'Padding',[0 0 0 1])
dropoutLayer
fullyConnectedLayer(1)
regressionLayer];
% Specify training options.
opts = trainingOptions('sgdm', ...
'MaxEpochs',1500, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',{GlucoseReadingsVal,val_GR_output},...
'LearnRateDropFactor',0.2,...
'LearnRateDropPeriod',5,...
'ExecutionEnvironment', 'cpu', ...
'ValidationPatience',Inf);
%% Train network
%net = trainNetwork(XTrain,Trainoutfinal,layers,opts);
yc = train_GR_output(:);
net1 = trainNetwork(GlucoseReadingsTrain,yc,layers,opts);
%% Compare against testing Data
Ypredicted = predict(net1, GlucoseReadingsTest)
predictionError = test_GR_output - GR_outputpredicted;
squares = predictionError.^2;
rmse = sqrt(mean(squares))
figure
scatter(GR_outputpredicted, test_GR_output,'+')
title ('True value vs Predicted Value')
xlabel ("Predicted Value")
ylabel ("True Value")
hold on
plot([-3 3], [-7 7], 'b--')
  2 Comments
Walter Roberson
Walter Roberson on 22 Feb 2022
You are still making mistakes in selecting your training, validation, and test set.
Create a variable which is the number of training, and index 1:numtrain instead of 1:17.
Create a variable which is the number of validations, and index numtrain+1:numtrain+numval instead of 18:21
Create a variable which is the number of test, as size(GlocuseReadings_T,1) - numtrain - numval, and index with numtrain+numval+1:numtrain+numval+numtest instead of 19:24
And in code like [1438,1,1,17] replace the 17 with numtrain, and replace the 4's with numval
fileurl = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/902755/GlucoseReadings.mat';
%Import/Upload data
tname = tempname() + ".mat";
urlwrite(fileurl, tname);
data = load(tname);
GR_output = data.GR_output;
GlucoseReadings = data.GlucoseReadings;
% change to label vector
CS = categories(categorical(GR_output));
Z1 = []; Z2 = [];
for i = 1 : length(GR_output)
Z1(i,1) = find(GR_output(i)==CS);
end
%for i = 1 : length(Y2)
%Z2(i,1) = find(Y2(i)==CS);
%end
Yo1 = GR_output;
%Yo2 = Y2;
GR_output= Z1;
%Y2 = Z2;
%transposing glucose data
GlucoseReadings_T = GlucoseReadings';
%Shuffling data to take randomly
rand('seed', 0)
ind = randperm(size(GlucoseReadings_T, 1));
GlucoseReadings_T = GlucoseReadings_T(ind, :);
GR_output = GR_output(ind);
%Separating data in training, validation and testing data
GlucoseReadings_train = GlucoseReadings_T;
%Partioning data for training 70%
train_GlucoseReadings = GlucoseReadings_train(1:17,:);
%Corresponding X(input) data to Y(output) data
train_GR_output = GR_output(1:17);
%reshaping data into 4D array
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1438,1,1,17]));
%Separating and partioning for validation data 15%
val_GlucoseReadings = GlucoseReadings_train(18:21,:);
%Corresponding X(input) data to Y(output) data
val_GR_output = GR_output(18:21);
%reshaping data into 4D array
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1438,1,1,4])); %Train data
%Separating and partioning for test data 15%
test_GlucoseReadings = GlucoseReadings_train(19:24,:);
%Corresponding X(input) data to Y(output) data
test_GR_output = GR_output(19:24);
%reshaping data into 4D array
whos
Name Size Bytes Class Attributes CS 2x1 238 cell GR_output 24x1 192 double GlucoseReadings 1438x24 276096 double GlucoseReadingsTrain 1438x1x1x17 195568 double GlucoseReadingsVal 1438x1x1x4 46016 double GlucoseReadings_T 24x1438 276096 double GlucoseReadings_train 24x1438 276096 double Yo1 1x26 1518 string Z1 26x1 208 double Z2 0x0 0 double ans 1x47 94 char data 1x1 277950 struct fileurl 1x89 178 char i 1x1 8 double ind 1x24 192 double test_GR_output 6x1 48 double test_GlucoseReadings 6x1438 69024 double tname 1x1 308 string train_GR_output 17x1 136 double train_GlucoseReadings 17x1438 195568 double val_GR_output 4x1 32 double val_GlucoseReadings 4x1438 46016 double
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1438,1,1,4])); %Train data
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
%% NETWORK ARCHITECTURE
layers = [imageInputLayer([1438 1 1]) % Creating the image layer
convolution2dLayer([102 1],3,'Stride',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2,'Padding',[0 0 0 1])
dropoutLayer
fullyConnectedLayer(1)
regressionLayer];
% Specify training options.
opts = trainingOptions('sgdm', ...
'MaxEpochs',1500, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',{GlucoseReadingsVal,val_GR_output},...
'LearnRateDropFactor',0.2,...
'LearnRateDropPeriod',5,...
'ExecutionEnvironment', 'cpu', ...
'ValidationPatience',Inf);
%% Train network
%net = trainNetwork(XTrain,Trainoutfinal,layers,opts);
yc = train_GR_output(:);
net1 = trainNetwork(GlucoseReadingsTrain,yc,layers,opts);
%% Compare against testing Data
Ypredicted = predict(net1, GlucoseReadingsTest)
predictionError = test_GR_output - GR_outputpredicted;
squares = predictionError.^2;
rmse = sqrt(mean(squares))
figure
scatter(GR_outputpredicted, test_GR_output,'+')
title ('True value vs Predicted Value')
xlabel ("Predicted Value")
ylabel ("True Value")
hold on
plot([-3 3], [-7 7], 'b--')

Sign in to comment.

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