Need help with Sequence input Deep Learning toolbox error

2 visualizaciones (últimos 30 días)
Eva
Eva el 16 de Jun. de 2023
Comentada: Ben el 20 de Jun. de 2023
I am trying to achieve a script to execute sequence-to-label(response) classification.
I am having a problem with how the function trainNetwork wants the input data packaged.
I am trying to figure out what to do with XTrain or XTrainTest, to get the 'trainNetwork' command to accept the input data.
I am receiving the following error: 'Error using trainNetwork
Invalid training data. Predictors must be a numeric array, a datastore, or a table. For networks with sequence input, predictors can also be a cell array of sequences. '
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
clear all;
close all;
clc
%--------------------------------------------------------------------------
% Generating A matrix
NewDimension = uint16(200);
mu = 5;
std = 2.1;
A = normrnd(mu, std, [NewDimension 784] );
%--------------------------------------------------------------------------
N_sample = 60000;
N_test=10000;
%XTrain = zeros(28,28,1,N_sample);
XTrain = zeros(1,NewDimension,1,N_sample);
XTrainTest = cell(N_sample,1);
YTrain=zeros(N_sample,1);
% Please dowload the MNIST data set from http://yann.lecun.com/exdb/mnist/
% and unzip.
fidimg1=fopen('train-images.idx3-ubyte','rb');
fidimg2=fopen('train-labels.idx1-ubyte','rb');
[img,count]=fread(fidimg1,16); % table head
[imgInd,count1]=fread(fidimg2,8); %table head
for k=1:N_sample
[im,~]=fread(fidimg1,[28,28]);
ind=fread(fidimg2,1);
clear testsave, clear testmult
testsave = reshape(im, 784, 1);
testmult = A*testsave;
XTrain(1,1:NewDimension,1,k)=transpose(testmult);
XTrainTest{k} = testmult';
%XTrainTest{k} = double(transpose(testmult));
%XTrain(:,:,1,k)=im';
YTrain(k)=ind;
end
fclose(fidimg1);
fclose(fidimg2);
YTrain=categorical(YTrain);
%-------------------------------------------------------------------------
XTest = zeros(1,NewDimension,N_test);
%XTest = zeros(1,784,1,N_test);
YTest=zeros(N_test,1);
fidimg1=fopen('t10k-images.idx3-ubyte','rb');
fidimg2=fopen('t10k-labels.idx1-ubyte','rb');
[img,count]=fread(fidimg1,16);
[imgInd,count1]=fread(fidimg2,8);
for k=1:N_test
[im,~]=fread(fidimg1,[28,28]);
ind=fread(fidimg2,1);
clear testsave, clear testmult
testsave = reshape(im, 784, 1);
testmult = A*testsave;
XTest(1,1:NewDimension,k)=transpose(testmult);
%XTest(:,:,1,k)=im';% training set building
YTest(k)=ind;
end
fclose(fidimg1);
fclose(fidimg2);
YTest=categorical(YTest);
numHiddenUnits = 400;
numClasses = 10;
numFeatures = NewDimension;
% Modify XTrain and XTest to be 2-D arrays
XTrain = reshape(XTrain, [NewDimension, N_sample]);
%YTrain = transpose(YTrain);
%XTest = reshape(XTest, [NewDimension, N_test]);
layers = [
featureInputLayer(numFeatures)
fullyConnectedLayer(100) % number of neurons in the first hidden layer
batchNormalizationLayer
reluLayer
fullyConnectedLayer(50) % number of neurons in the second hidden layer
batchNormalizationLayer
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
maxEpochs = 5;
miniBatchSize = 20;
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(XTrainTest,YTrain,layers,options);
  3 comentarios
Eva
Eva el 16 de Jun. de 2023
I think I seemed to discover the fault in this simulation, and it had to do with my layer stack up, it was defined for featureinputLayers and not for sequenceInputLayer:
The following seems to allow it to train:
layers = [
sequenceInputLayer(numFeatures)
convolution1dLayer(filterSize, numFilters, 'Padding', 'causal')
reluLayer
layerNormalizationLayer
convolution1dLayer(filterSize, 2*numFilters, 'Padding', 'causal')
reluLayer
layerNormalizationLayer
globalAveragePooling1dLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer
];
Ben
Ben el 20 de Jun. de 2023
In general you should be able to use both featureInputLayer and sequenceInputLayer for this problem, by interpreting the image as either a vector 784 features, or a sequence of 784 features. But if you want to use convolution1dLayer you'll need to use sequenceInputLayer since convolution1dLayer operates over the sequence dimension.
For the datastores for sequence data, this example might be a good starting point using only built-in datastores, and this example includes a custom sequenceDatastore implementation that you can look at to get an idea of how you might write your own datastore for this task.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

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

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by