Attempting to correctly code the validation and testing data into network

1 view (last 30 days)
Nathaniel Porter
Nathaniel Porter on 15 Dec 2021
Answered: yanqi liu on 16 Dec 2021
clc; clear all; close all;
%Import/Upload data
load generated_data.mat
Error using load
Unable to find file or directory 'generated_data.mat'.
%transposing glucose data
X1_T = X1';
%transposing insulin data
X2_T = X2';
%Separating data in training, validation and testing data
%Shuffling data
rand('seed', 0)
ind = randperm(size(X1_T, 1));
X1_T = X1_T(ind, :);
Y1 = Y1(ind);
%-----------------------------------------------------------------------
X1_train = X1_T;
%Partioning data for training
train_X1 = X1_train(1:120,:);
train_Y1 = Y1(1:120);
%DataParts = zeros(size(Train_inputX1,1), size(Train_inputX1,2),1,2); %(4500,400,1,2)
%DataParts(:,:,:,1) = real(cell2mat(Train_inputX1));
%DataParts(:,:,:,2) = imag(cell2mat(Train_inputX1)) ;
XTrain=(reshape(train_X1, [120,1,1,2289])); %Train data
%Separating and partioning for validation data
val_X1 = X1_train(121:150,:);
val_Y1 = Y1(121:150);
XVal=(reshape(val_X1', [2289,1,1,30])); %Train data
%val_Y1 = Y1(121:150);
%XVal=(reshape(val_X1, [30,1,1,2289])); %Train data
%Separating and partioning for test data
test_X1 = X1_train(151:180,:);
%test_Y1 = Y1(151:180);
%XTest=(reshape(test_X1', [30,1,1,2289]));
%Xtest=(reshape(test_X1, [120,1,1,2289])); %Train data
%Separating data in training, validation and testing data
%X2_train = X2_T;
%Partioning data for training
%train_X2 = X2_train(1:120,:);
%Separating and partioning for validation data
%val_X2 = X2_train(121:150,:);
%Separating and partioning for test data
%test_X2 = X2_train(151:180,:);
%The number of features chosen to be two representing both glucose and
%insulin
numFeatures = size(X1_T,2);
% number of hidden units represent the size of the data
numHiddenUnits = 180;
%number of classes represent different patients normal,LIS,type2....
numClasses = length(categories(categorical(Y1)));
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',60, ...
'GradientThreshold',2, ...
'Verbose',false, ...
'ValidationData',{XVal,categorical(val_Y1)},...
'LearnRateDropFactor',0.2, ...
'LearnRateDropPeriod',5, ...
'Plots','training-progress');
net = trainNetwork(X1_train',categorical(Y1),layers,options);

Answers (1)

yanqi liu
yanqi liu on 16 Dec 2021
clc; clear all; close all;
load generated_data.mat
% 2289*180
% 6 classes
X1_T = X1';
X2_T = X2';
rand('seed', 0)
ind = randperm(size(X1_T, 1));
X1_T = X1_T(ind, :);
Y1 = categorical(Y1(ind));
% Split Data
X1_train = X1_T;
train_X1 = X1_train(1:120,:);
train_Y1 = Y1(1:120);
% Data Batch
XTrain=(reshape(train_X1', [2289,120]));
val_X1 = X1_train(121:150,:);
val_Y1 = Y1(121:150);
XVal=(reshape(val_X1', [2289,30]));
test_X1 = X1_train(151:180,:);
test_Y1 = Y1(151:180);
XTest=(reshape(test_X1', [2289,30]));
numFeatures = size(X1_T,2);
% number of hidden units represent the size of the data
numHiddenUnits = 180;
%number of classes represent different patients normal,LIS,type2....
numClasses = length(categories(categorical(Y1)));
layers = [ ...
sequenceInputLayer(numFeatures)
bilstmLayer(numHiddenUnits,'OutputMode','sequence')
dropoutLayer(0.1)
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',2000, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'ValidationData',{XVal, val_Y1},...
'LearnRateDropFactor',0.2, ...
'LearnRateDropPeriod',5, ...
'Plots','training-progress');
% Train
net = trainNetwork(XTrain,train_Y1,layers,options);
% Test
miniBatchSize = 27;
YPred = classify(net,XTest, ...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment', 'cpu');
acc = mean(YPred(:) == categorical(test_Y1(:)))
figure
t = confusionchart(categorical(test_Y1(:)),YPred(:));

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by