Error using zeros Size inputs must be scalar.

fileID = 'kddcup.testdata.unlabeled_10_percent.txt';
input= xlsread('KDDTrain.xls');
target = fopen(fileID);
%target= load('kddcup.testdata.unlabeled_10_percent.txt');
testData= xlsread('KDDTest.xls');
%% Formating of dataset
% p=T(:,0.70)';
% t=T(:,0.30)';
ivp=input';
ivt=target';
testinputs=testData';
[trainx, testy]=mapminmax(ivp,ivt);
%% DataSet dimensions
numberOfClasses = max(input);
numberOfRecords = size(input, 1);
numberOfFeatures = size(input, 2) - 1;
% dataSet = correct
%% Max - Win target Preparation
target = zeros(numberOfClasses, numberOfRecords);
Ind = sub2ind(size(target), input', 1:numberOfRecords);
target(Ind) = 1;
target = target*2-1;
%% Multilayer Perceptron Network Initialization
mlp = feedforwardnet([5 7]);
mlp.layers{1}.transferFcn = 'tansig';
mlp.layer{2}.transferFcn = 'tansig';
mlp.trainFcn = 'trainlm'; % Training Function
mlp.performFcn = 'mse'; % performance Function
%% Training parameters
mlp.train.Param.min_grad = 10^-7;
mlp.train.Param.epochs = 1000;
mlp.train.Param.time = inf;
mlp.train.Param.goal = 0;
mlp.train.Param.showWindow = true;
%% Training the Neural Network
mlp = train(mlp, input(:,numberOfFeatures)', target);
%% Testing the Neural Network on the training dataset
[~, outputs] = max(mlp(input(:, 1:numberOfFeatures)'));
%% Calculate the Accuracy
accuracy = lenght(find(output == input'))/numberOfRecords
Please,
I need help with the code line below.
And a walk through the code to see if the code will pull through else point to me where my code is wrong and possible alterations/ changes.
I have been battling with this code for a long time now but couldn't seem to have pulled through.
this line seem to pull the above error in question:
target = zeros(numberOfClasses, numberOfRecords);

9 Comments

target = zeros(numberOfClasses,numberOfRecords, 'like',h);
At that point, h is undefined in the above code.
Guillaume
Guillaume on 21 Sep 2019
Edited: Guillaume on 21 Sep 2019
We know that numberOfRecords will be an integer (since it's the number of rows in the file), but there's absolutely no guarantee that numberOfClasses, the max of the data, is integer.
So, have you looked at the value of numberOfClasses? what is it?
@ dpb,
I have remove the expression where I assigned value for 'h' because, it is not giving me what I want.
target = zeros(numberOfClasses,numberOfRecords, 'like',h);
This is the code I am using and its still giving thesame error message.
target = zeros(numberOfClasses, numberOfRecords);
numberOfClasses is a 1x41 double.
output is = 0.0000 0.0000 0.0000 0.0000 0.0000 etc
Please depending on you guys... and also look through the code if there is still any issue with any part of it.
This is the code I am using
target = zeros(numberOfClasses, numberOfRecords);
numberOfClasses is a 1x41 double [apparently full of 0s]
Well, don't you see there's a problem right there? What are you hoping that
zero(vector_full_of_0s, scalar)
is going to do?
Please, any suggestions...
target = zeros(numberOfClasses, numberOfRecords);
to make it this
scalar, scalar
Trying to develop and intrusion detection system...
numberOfClasses = max(input);
numberOfRecords = size(input, 1);
numberOfFeatures = size(input, 2) - 1;
% dataSet = correct
%% Max - Win target Preparation
target = zeros(numberOfClasses, numberOfRecords);
Ind = sub2ind(size(target), input', 1:numberOfRecords);
target(Ind) = 1;
target = target*2-1;
How can I best apply these lines of code.
the table has 41 columns with about 65367 rows...
Thank You for your contributions
numberOfClasses = max(input);
We as outside observers have no reason to expect that input contains only non-negative integers. But as the first step I suggest
numberOfClasses = max(input(:));
to get the overall maximum instead of the column-by-column maximum.
Even then I would suggest that it would be more likely that one column of the input contains the information about class.
target = fopen(fileID);
Did you notice that you are not reading from the file? You are only opening it and using the file identifier (which would most often be 3) as the target number.
Thank you for your response. I saw that I wasn't actually reading from the file. After I rectified that, I encountered some other errors I am trying to debug now.
I have tried to trim down some codes that looks like excesses. Its shown below:
clc;
clear;
close all;
%% Load dataset and plot graph
fileID = 'kddcup.testdata.unlabeled_10_percent.txt';
input= xlsread('KDDTrain.xls');
tg = fopen(fileID,'r');
%target = fread(tg);
%target= load('kddcup.testdata.unlabeled_10_percent.txt');
testData= xlsread('KDDTest.xls');
%% use 20% of input dat
[inp ts]= size(input);
P = 0.20 ;
idy = randperm(inp);
I = input(idy(1:round(P*inp)),:) ;
numberOfClasses = max(I(:,end));
numberOfRecords = size(I, 1);
numberOfFeatures = size(I, 2) - 1;
% dataSet = correct
%% Max - Win target Preparation
target = zeros(numberOfClasses,numberOfRecords);
Ind = sub2ind(size(target), I', 1:numberOfRecords);
target(Ind) = 1;
target = target*2-1;
%% Multilayer Perceptron Network Initialization
mlp = feedforwardnet([5 7]);
mlp.layers{1}.transferFcn = 'tansig';
mlp.layer{2}.transferFcn = 'tansig';
mlp.trainFcn = 'trainlm'; % Training Function
mlp.performFcn = 'mse'; % performance Function
%% Training parameters
mlp.train.Param.min_grad = 10^-7;
mlp.train.Param.epochs = 1000;
mlp.train.Param.time = inf;
mlp.train.Param.goal = 0;
mlp.train.Param.showWindow = true;
%% Training the Neural Network
mlp = train(mlp, input(:,numberOfFeatures)', target);
%% Testing the Neural Network on the training dataset
[~, outputs] = max(mlp(input(:, 1:numberOfFeatures)'));
%% Calculate the Accuracy
accuracy = lenght(find(output == input'))/numberOfRecords
And I encountered this error:
Error using sub2ind (line 43)
Out of range subscript.
Error in mmllpp (line 27)
Ind = sub2ind(size(target), I', 1:numberOfRecords);
Still debugging though. If i can get an improve/better way of training this KDD CUP 99 dataset for Intrusion Detection System, will appreciate it. I do not have much time and I'm new with MATLAB.
You're stll not reading anything from your text file, so it's not clear what it's doing there.
As I wrote in my answer for that sub2ind call to work all inputs must be the same size/shape (you appear to have fixed that) and must be valid subscripts: strictly positive integer less than or equal to the size of the corresponding dimension. The less than or equal is guaranteed with your code, so clearly it's the strictly positive that your code is violating.
This is where I have debugged to:
clc;
clear;
close all;
%% Load dataset and plot graph
fileID = 'kddcup.testdata.unlabeled_10_percent.txt';
trainingData= xlsread('KDDTrain.xls');
tg = fopen(fileID,'r');
target = fread(tg);
testData= xlsread('KDDTest.xls');
[inp ts]= size(trainingData);
P = 0.20 ;
idy = randperm(inp);
I = trainingData(idy(1:round(P*inp)),:) ;
numberOfClasses = max(I(:,end));
numberOfRecords = size(I, 1);
numberOfFeatures = size(I, 2) - 1;
[targ tn]= size(target);
P = 0.20 ;
idx = randperm(targ);
T = target(idx(1:round(P*targ)),:) ;
%% Multilayer Perceptron Network Initialization
mlp = feedforwardnet([5 7]);
mlp.layers{1}.transferFcn = 'tansig';
mlp.trainFcn = 'trainlm'; % Training Function
mlp.performFcn = 'mse'; % performance Function
%% Training parameters
mlp.trainParam.min_grad = 10^-7;
mlp.trainParam.epochs = 1000;
mlp.trainParam.time = inf;
mlp.trainParam.goal = 0;
mlp.trainParam.showWindow = true;
%% Training the Neural Network
mlp = train(mlp, I(:,numberOfFeatures)', T);
view(mlp)
%% Testing the Neural Network on the training dataset
[~, outputs] = max(mlp(I(:, 1:numberOfFeatures)'));
%% Calculate the Accuracy
accuracy = lenght(find(output == I'))/numberOfRecords
But, still have some errors:
Error using network/train (line 340)
Inputs and targets have different numbers of
samples.
Error in mmllpp (line 54)
mlp = train(mlp, I(:,numberOfFeatures)', T);

Sign in to comment.

 Accepted Answer

Yes, we have no idea what you meant to do, so can't really tell you how to fix the problem. Currently you're calculating the maximum of each column of your excel file and then ... trying to create a zero matrix using the size of each of the column as the first dimension of the matrix. Obviously the size of a dimension must be a single number, and the size must be a positive integer number. Perhaps, you meant to use only one column. Perhaps, you didn't mean to use the max, perhaps you meant do do something else entirely.
It's the same problem with target as Walter pointed out. You fopen a file and assign the file identifier that it returns to target. A file identifier is only useful if you're going to use it to read the file later. The only thing you do with it is transpose it which won't do anything since it's scalar, assign it to a variable then try to replace it by a matrix of 0. Again, there's a problem there but since we don't know what you were trying to do, we can't say how to fix it. Possibly, target was meant to be the content of the file (which you never read). How to read the file depends on how it's formatted.
While we're at it, I suggest you don't use input as a variable name. For a start, it would prevent you from using matlab's input function, but more importantly it's a meaningless name. Everything you start with is an input. trainingdata would be a much better variable name.
Even once you've solved the above problem, you'll then have a problem with the next line
Ind = sub2ind(size(target), input', 1:numberOfRecords);
since input appears to be a 2D matrix, and 1:numberOfRecords is a vector, the call will fail. In addition since your input clearly contain zeros and non-integer it makes no sense to use that as a subscript. Again, no idea what you're trying to do there.

2 Comments

Thank you for your response. Before seeing your response, I have encountered it already. for the variable name 'input' I will change it immediately, thanks.
This is error message:
Error using network/train (line 340)
Inputs and targets have different numbers of
samples.
Error in mmllpp (line 54)
mlp = train(mlp, I(:,numberOfFeatures)', T);
Still debugging though. If i can get an improve/better way of training this KDD CUP 99 dataset for Intrusion Detection System, will appreciate it. I do not have much time and I'm new with MATLAB.
Ones again, thank you for your contributions. I am grateful.
Inputs and targets have different numbers of samples.
One of the most common causes of that is passing in data that is the transpose of the order that the routine expects. The various neural network functions are not always consistent with each other as to whether samples should be across rows or down columns.

Sign in to comment.

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!