Not enough input arguments - trainNetwork

Hi again,
I have attempted to make a 3 layer neural network, it is to classify Iris plants, I have received an error which states:
Error in seriesnetwork (line 34)
net = trainNetwork(trainset,layers,options);
Caused by:
Error using nnet.internal.cnn.trainNetwork.DLTInputParser>iParseInputArguments
Not enough input arguments.
I don't know what I am missing/if I have put incorrect values into the code (see below)
clear
clc
%% importing iris data
test = Iris_data;
%% Labelling iris data as 1,2,3 (setosa, versicolor, virginica)
label = zeros(150,1);
label(1:50,:) = 1;
label(51:100,:) = 2;
label(101:150,:) = 3;
test(:,5) = label;
k = randperm(150,50);
trainset = test(k(1:50),:);
test(k,:) = [];
clear k label % clearing variables
%%
numFeatures = size(test)-1;
numFeatures = numFeatures(2);
numClasses = 3;
layers = [
featureInputLayer(numFeatures,'Normalization','zscore')
fullyConnectedLayer(3)
batchNormalizationLayer
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
miniBatchSize = 25;
options = trainingOptions('adam', ...
'MiniBatchSize',miniBatchSize, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false);
net = trainNetwork(trainset,layers,options);
YPred = classify(net,trainset,'MiniBatchSize',miniBatchSize);
YTest = test(:,5);
accuracy = sum(Ypred == YTest)/numel(YTest)
attached is the iris data

Réponses (2)

Walter Roberson
Walter Roberson le 24 Août 2022

0 votes

When you pass numeric data as the first parameter to trainnetwork(), then you need to pass four parameters, with responses as the second parameter.

4 commentaires

MILLER BIGGELAAR
MILLER BIGGELAAR le 24 Août 2022
what would those 4 parameters be?
Steven Lord
Steven Lord le 24 Août 2022
The Syntax section of the trainNetwork documentation page lists the various syntaxes with which you can call trainNetwork. If you click on one of the syntaxes in that section it will bring you to the description of that syntax in the Description section. If that description matches what you're trying to do, from there you can click on the input arguments to bring you to the entry in the Input Arguments section that describes the purpose of that argument and what that argument is allowed to be (double, single, integer, cell array, etc.)
net = trainNetwork(trainset, responses, layers, options);
for the case where trainset is a numeric array rather than a dataset or table
MILLER BIGGELAAR
MILLER BIGGELAAR le 24 Août 2022
Modifié(e) : MILLER BIGGELAAR le 24 Août 2022
I have an error as follows:
Error using trainNetwork
Invalid training data table for classification. Predictors must be in the first column of the table, as a cell array of image paths or images. Responses must be after the first column, as categorical labels.
Does this mean that for my training data, using the trainNetwork() function, the response variable should be the classes that the data belong to? From mathworks:
N-by-1 categorical vector of labels, where N is the number of observations.
so if my training set is 50 large, i need a 50x1 categorical array which contains the data's class?

Connectez-vous pour commenter.

David Ho
David Ho le 24 Août 2022
Modifié(e) : David Ho le 24 Août 2022
As Walter has pointed out, if your data is in a numeric array, you need to pass predictors and responses separately.
For a classification task, you also need to specify the labels as a categorical array: after you set the labels on line 9 you can convert them to categorical by inserting the line
labels = categorical(labels);
Then you need to split the labels using the same partitioning as you split your predictors, rather than concatenating them with the predictors: i.e. remove line 10, and add something like
labelsTrain = labels(k(1:50));
labelsTest = labels(k(51:end));
I believe this should help you to train the network as expected, but if you are still experiencing issues, it would be great if you could upload the data you are using so that we can run your code.

Catégories

En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2022a

Modifié(e) :

le 24 Août 2022

Community Treasure Hunt

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

Start Hunting!

Translated by