Error using zeros Size inputs must be scalar.
Show older comments
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
dpb
on 21 Sep 2019
target = zeros(numberOfClasses,numberOfRecords, 'like',h);
At that point, h is undefined in the above code.
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?
Williams Ofor
on 21 Sep 2019
Guillaume
on 21 Sep 2019
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?
Williams Ofor
on 22 Sep 2019
Walter Roberson
on 22 Sep 2019
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.
Williams Ofor
on 23 Sep 2019
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
on 23 Sep 2019
Edited: Williams Ofor
on 24 Sep 2019
Accepted Answer
More Answers (0)
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!