Number of observations in X and Y disagree.
1 view (last 30 days)
Show older comments
clc; clear all; close all;
%Import/Upload data
load Projectdata.mat
% change to label vector
CS = categories(categorical(GR_output));
CS1 = categories(categorical(INS_output));
Z1 = []; Z2 = [];
for i = 1 : length(GR_output)
Z1(i,1) = find(GR_output(i)==CS);
end
for i = 1 : length(INS_output)
Z2(i,1) = find(INS_output(i)==CS1);
end
Yo1 = GR_output;
Yo2 = INS_output;
GR_output= Z1;
INS_output = Z2;
%transposing glucose data
GlucoseReadings_T = GlucoseReadings';
%transposing insulin data
InsulinReadings_T = InsulinReadings';
%Shuffling data to take randomly
rand('seed', 0)
ind = randperm(size(GlucoseReadings_T, 1));
GlucoseReadings_T = GlucoseReadings_T(ind, :);
GR_output = GR_output(ind);
ind = randperm(size(InsulinReadings_T, 1));
InsulinReadings_T = InsulinReadings_T(ind, :);
INS_output = INS_output(ind);
%Separating data in training, validation and testing data
GlucoseReadings_train = GlucoseReadings_T;
InsulinReadings_train = InsulinReadings_T;
%Partioning data for training 70%
train_GlucoseReadings = GlucoseReadings_train(1:84,:);
train_InsulinReadings = InsulinReadings_train(1:84,:);
%Corresponding X(input) data to Y(output) data
train_GR_output = GR_output(1:17);
train_INS_output = INS_output(1:84);
%reshaping data into 4D array
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1749,1,1,84]));
InsulinReadingsTrain=(reshape(train_InsulinReadings',[1758,1,1,84]));
%Separating and partioning for validation data 15%
val_GlucoseReadings = GlucoseReadings_train(85:102,:);
val_InsulinReadings = InsulinReadings_train(85:102,:);
%Corresponding X(input) data to Y(output) data
val_GR_output = GR_output(85:102);
val_INS_output = INS_output(85:102);
%reshaping data into 4D array
whos
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1749,1,1,18]));
InsulinReadingsVal=(reshape(val_InsulinReadings', [1758,1,1,18]));
%Separating and partioning for test data 15%
test_GlucoseReadings = GlucoseReadings_train(103:120,:);
test_InsulinReadings = InsulinReadings_train(103:120,:);
%Corresponding X(input) data to Y(output) data
test_GR_output = GR_output(103:120);
test_INS_output = INS_output(103:120);
%reshaping data into 4D array
GlucoseReadingsTest=(reshape(test_GlucoseReadings', [1749,1,1,18]));
InsulinReadingsTest=(reshape(test_InsulinReadings', [1758,1,1,18]));
%% NETWORK ARCHITECTURE
layers = [imageInputLayer([1758 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',1000, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',{GlucoseReadingsVal,val_GR_output,},...
'ValidationData',{InsulinReadingsVal,val_INS_output,},...
'LearnRateDropFactor',0.2,...
'LearnRateDropPeriod',5,...
'ExecutionEnvironment', 'cpu', ...
'ValidationPatience',Inf);
%% Train network
%net = trainNetwork(XTrain,Trainoutfinal,layers,opts);
yc = train_GR_output(:);
yc1 = train_INS_output(:);
net1 = trainNetwork(GlucoseReadingsTrain,yc,layers,opts);
net2 = trainNetwork(InsulinReadingsTrain,yc1,layers,opts);
%% Compare against testing Data
GR_outputpredicted = predict(net1, GlucoseReadingsTest)
INS_outputpredicted = predict(net1, InsulinReadingsTest)
predictionError = test_GR_output - GR_outputpredicted;
predictionError1 = test_INS_output - INS_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--')
scatter(INS_outputpredicted, test_INS_output,'+')
title ('True value vs Predicted Value')
xlabel ("Predicted Value")
ylabel ("True Value")
hold on
plot([-3 3], [-7 7], 'b--')
0 Comments
Accepted Answer
KSSV
on 4 Mar 2022
Edited: KSSV
on 4 Mar 2022
Your yc i.e. target for the respective input in the line:
net1 = trainNetwork(GlucoseReadingsTrain,yc,layers,opts);
is of dimension 17x1. Where as input is 1749x1x1x84. So there is a miss match and you got error. Where as the variables yc1 is of dimension 84x1 and this should work. This line should not through any error:
net2 = trainNetwork(InsulinReadingsTrain,yc1,layers,opts);
So you have to use net2. You may delete the line net1.
5 Comments
KSSV
on 4 Mar 2022
Now you messed up with the input: GlucoseReadingsTrain
It should be 1758x1x1x84, but it is 1749x1x1x84.
More Answers (1)
Walter Roberson
on 4 Mar 2022
Edited: Walter Roberson
on 4 Mar 2022
net2 = trainNetwork(train_GlucoseReadings,yc1,layers1,opts);
Your code uses a series of variables that are not well distinguished from each other in meaning, so it is easy to get wrong which ones you are talking about.
See Also
Categories
Find more on Recognition, Object Detection, and Semantic Segmentation 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!