Number of observations in X and Y disagree.

4 views (last 30 days)
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);
for i = 1 : length(INS_output)
Z2(i,1) = find(INS_output(i)==CS1);
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]));
%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
Name Size Bytes Class Attributes CS 2x1 248 cell CS1 2x1 248 cell GR_output 120x1 960 double GlucoseReadings 1749x120 1679040 double GlucoseReadingsTrain 1749x1x1x84 1175328 double GlucoseReadings_T 120x1749 1679040 double GlucoseReadings_train 120x1749 1679040 double INS_output 120x1 960 double InsulinReadings 1758x120 1687680 double InsulinReadingsTrain 1758x1x1x84 1181376 double InsulinReadings_T 120x1758 1687680 double InsulinReadings_train 120x1758 1687680 double Yo1 1x120 7296 string Yo2 1x120 7296 string Z1 120x1 960 double Z2 120x1 960 double i 1x1 8 double ind 1x120 960 double train_GR_output 17x1 136 double train_GlucoseReadings 84x1749 1175328 double train_INS_output 84x1 672 double train_InsulinReadings 84x1758 1181376 double val_GR_output 18x1 144 double val_GlucoseReadings 18x1749 251856 double val_INS_output 18x1 144 double val_InsulinReadings 18x1758 253152 double
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]));
layers = [imageInputLayer([1758 1 1]) % Creating the image layer
convolution2dLayer([102 1],3,'Stride',1)
maxPooling2dLayer(2,'Stride',2,'Padding',[0 0 0 1])
% Specify training options.
opts = trainingOptions('sgdm', ...
'MaxEpochs',1000, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ExecutionEnvironment', 'cpu', ...
%% Train network
%net = trainNetwork(XTrain,Trainoutfinal,layers,opts);
yc = train_GR_output(:);
yc1 = train_INS_output(:);
net1 = trainNetwork(GlucoseReadingsTrain,yc,layers,opts);
Error using trainNetwork (line 184)
Number of observations in X and Y disagree.
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))
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--')

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.
Nathaniel Porter
Nathaniel Porter on 4 Mar 2022
GlucoseReadingsTrain=(reshape(train_GlucoseReadings', [1758,1,1,84]));
Changed it but got this:
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.

Sign in to comment.

More Answers (1)

Walter Roberson
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.
  1 Comment
Nathaniel Porter
Nathaniel Porter on 4 Mar 2022
No well this is suppose to be my net two which are my insulin readings
net2 = trainNetwork(InsulinReadingsTrain,yc1,layers1,opts);

Sign in to comment.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by