Error using zeros Size inputs must be scalar.

10 views (last 30 days)
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
Guillaume
Guillaume on 23 Sep 2019
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.
Williams Ofor
Williams Ofor on 23 Sep 2019
Edited: Williams Ofor on 24 Sep 2019
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

Guillaume
Guillaume on 22 Sep 2019
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
Williams Ofor
Williams Ofor on 23 Sep 2019
Edited: Williams Ofor on 24 Sep 2019
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.
Walter Roberson
Walter Roberson on 24 Sep 2019
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)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!