Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
Show older comments
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
Nathaniel Porter
on 22 Feb 2022
KSSV
on 22 Feb 2022
GlucoseReadingsVal=(reshape(val_GlucoseReadings', [1438,1,1,18])); %Train data
The above line is not needed. You are trying to put some data aside for validation. The dimensions of this should be same as GlucoseReadingsTrain. If you have other data assign here, if not put some data aside from training data.
This line:
GlucoseReadingsTest=(reshape(GlucoseReadings_X1', [1438,1,1,18])); %Train data
From where the variable GlucoseReadings_X1 has come?
Nathaniel Porter
on 22 Feb 2022
Nathaniel Porter
on 22 Feb 2022
Accepted Answer
More Answers (0)
Categories
Find more on Visualization and Interpretability 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!