Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
7 views (last 30 days)
Show older comments
Nathaniel Porter
on 22 Feb 2022
Commented: Walter Roberson
on 22 Feb 2022
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
Accepted Answer
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
and checking we see that Yes, it is 4 x something
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1438,1,1,18])); %Train data
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
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
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1438,1,1,4])); %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--')
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!