Trying to create neural network but getting a NaN error from dataset

1 view (last 30 days)
%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,:);
%Separating and partioning for validation data
val_X1 = X1_train(121:150,:);
%Separating and partioning for test data
test_X1 = X1_train(151:180,:);
%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 = 2;
% number of hidden units represent the size of the data
numHiddenUnits = 180;
%number of classes represent different patients normal,LIS,type2....
numClasses = 6;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits,'OutputMode','last')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',60, ...
'GradientThreshold',2, ...
'Verbose',0, ...
'Plots','training-progress');
isnan(train_X1)
net = trainNetwork(train_X1,Y1,layers,options);
  3 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Dec 2021
net = trainNetwork(train_X1, string(Y1), layers, options);
However:
Error using trainNetwork (line 184)
The training sequences are of feature dimension 120 but the input layer expects sequences of feature dimension 2.
And indeed you coded
numFeatures = 2;
If you are only wanting to pass in two features then you are going to have to extract those two features out of your 120 x 2289 array.

More Answers (2)

Nathaniel Porter
Nathaniel Porter on 2 Dec 2021
%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,:);
%Separating and partioning for validation data
val_X1 = X1_train(121:150,:);
%Separating and partioning for test data
test_X1 = X1_train(151:180,:);
%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 = 2;
% number of hidden units represent the size of the data
numHiddenUnits = 180;
%number of classes represent different patients normal,LIS,type2....
numClasses = 6;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits,'OutputMode','last')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',60, ...
'GradientThreshold',2, ...
'Verbose',0, ...
'Plots','training-progress');
isnan(train_X1)
net = trainNetwork(train_X1,Y1,layers,options);

yanqi liu
yanqi liu on 2 Dec 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);
%Separating and partioning for validation data
val_X1 = X1_train(121:150,:);
%Separating and partioning for test data
test_X1 = X1_train(151:180,:);
%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('adam', ...
'MaxEpochs',60, ...
'GradientThreshold',2, ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(X1_train',categorical(Y1),layers,options);

Community Treasure Hunt

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

Start Hunting!