Data in 4D array but still getting error and network issue
1 次查看(过去 30 天)
显示 更早的评论
clc; clear all; close all;
%Import/Upload data
load generated_data.mat
%transposing glucose data
X1_T = X1';
%transposing insulin data
X2_T = X2';
%Separating data in training, validation and testing data
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,:);
XVal=(reshape(val_X1, [30,1,1,2289])); %Train data
%Separating and partioning for test data
test_X1 = X1_train(151:180,:);
%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,'OutputMode','sequence')
fullyConnectedLayer(numClasses)
softmaxLayer
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',60, ...
'GradientThreshold',2, ...
'Verbose',0, ...
'LearnRateDropFactor',0.2, ...
'LearnRateDropPeriod',5, ...
'Plots','training-progress');
net = trainNetwork(X1_train',categorical(Y1),layers,options);
2 个评论
Voss
2021-12-10
Please upload the .mat file so that others can run the code. Also, if running the code generates an error message, please specify the full text of that error message; if not, please explain why you say there is an error (e.g., results not as expected).
回答(2 个)
yanqi liu
2021-12-13
编辑:yanqi liu
2021-12-13
yes,sir,may be it the same data and problem,so here use target split data to train and test,such as
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 = 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,1,1,120]));
val_X1 = X1_train(121:150,:);
val_Y1 = Y1(121:150);
XVal=(reshape(val_X1', [2289,1,1,30]));
test_X1 = X1_train(151:180,:);
test_Y1 = Y1(151:180);
XTest=(reshape(test_X1', [2289,1,1,30]));
% CNN
layers = [imageInputLayer([2289 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(6)
softmaxLayer
classificationLayer];
% Specify training options.
opts = trainingOptions('adam', ...
'MaxEpochs',200, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',{XVal,categorical(val_Y1)},...
'ExecutionEnvironment', 'cpu', ...
'ValidationPatience',Inf);
% Train
yc = categorical(train_Y1);
net1 = trainNetwork(XTrain,yc,layers,opts);
% Test
miniBatchSize = 27;
YPred = classify(net1,XTest, ...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment', 'cpu');
acc = mean(YPred(:) == categorical(test_Y1(:)))
figure
t = confusionchart(categorical(test_Y1(:)),YPred(:));
acc =
0.9667
0 个评论
Sahil Jain
2021-12-21
编辑:Sahil Jain
2021-12-21
Hi Nathaniel. I am assuming your data consists of 180 sequences of length 2289 each. For vectors, the "sequenceInputLayer" expects inputs of size (num_features x sequence_length). For this case, the number of features is 1 so you should reshape "XTrain" to a cell array containing 120 cells where each cell is a 1x2289 double. This can be done by replacing your definition of "XTrain" with the following:
XTrain=(reshape(train_X1, [120,2289])); %Train data
XTrain = num2cell(XTrain, 2);
Correspondingly, set the value of variable "numFeatures" to 1. As your ouput variables are classes, you should replace the "regressionLayer" with a "classificationLayer". Also, make sure that the corresponding Y variable is a categorical column. This can be done using
YTrain = categorical(train_Y1')
Lastly, pass the newly created "XTrain" and "YTrain" to the the trainNetwork function.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!