Data in 4D array but still getting error and network issue

1 visualización (últimos 30 días)
Nathaniel Porter
Nathaniel Porter el 10 de Dic. de 2021
Editada: Sahil Jain el 21 de Dic. de 2021
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 comentarios
Voss
Voss el 10 de Dic. de 2021
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).
Nathaniel Porter
Nathaniel Porter el 13 de Dic. de 2021
This is the error being produced:
Error using trainNetwork (line 184)
The training sequences are of feature dimension 180 but the input layer expects sequences of feature
dimension 2289.
Error in RNN_CW3 (line 52)
net = trainNetwork(X1_train,categorical(Y1),layers,options);
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);
Error using trainNetwork (line 184)
The training sequences are of feature dimension 180 but the input layer expects sequences of feature dimension 2289.
And my dat file is also attached. Any help will be appreciated

Iniciar sesión para comentar.

Respuestas (2)

yanqi liu
yanqi liu el 13 de Dic. de 2021
Editada: yanqi liu el 13 de Dic. de 2021
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

Sahil Jain
Sahil Jain el 21 de Dic. de 2021
Editada: Sahil Jain el 21 de Dic. de 2021
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.

Categorías

Más información sobre Image Data Workflows en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by