trainlm predict value & switch

output of trained network is y from
load data
Tb = readtable('train1.csv','PreserveVariableNames',true)
x = Tb(:,(1:562))%data.simplefitInputs';
t = Tb(:,(563:563));%data.simplefitTargets';
[net,tr] = train(net,x,t);
y = net(x);
as target were classes 1,2,3,4,5,6
why output is 1.123 , 4.454, 5.6575 etc not same classes as input?
and how to convert output y to predicted value ?
YPredicted = classify(net.myNet,y)
returns error:
SWITCH expression must be a scalar or a character vector.
Error in network/subsref (line 173)
switch (subs)
my final output would be confusionmatrix
plotconfusion(YTest,YPredicted)

回答(1 个)

Hi
In my understanding, you want classification output and you are getting regression output. Convert the variable 't' into a categorical array before training the model and check if it is working or not, i.e
t = categorical(Tb(:,(563:563)));

3 个评论

apologise it will not work
code
%Train= load('TrainSetArray.mat')
%Target = load('TargetSet.mat')
% avaliable under
%https://drive.google.com/open?id=1CzfnIY5DZqcu8Vt-zxM3I6sVLnzLhwK5
x = Train';
t = categorical(table2array(Tb(:,(563:563))));
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Fitting Network
hiddenLayerSize = 1;
net = fitnet(hiddenLayerSize,trainFcn);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t);
%plot confusion chart across categories
YPredicted = classify(net.myNet,y)
plotconfusion(y,YPredicted)
Error in nntraining.setup (line 77)
[net,data,tr,err] = setupPerWorker(net,trainFcn,X,Xi,Ai,T,EW,enableConfigure);
train and target mat files avaliable under below link
Hi
The below is the working code for classification of your application. Hope it will helps.
Train= load('TrainSetArray.mat')
Target = load('TargetSetArray.mat')
% avaliable under
%https://drive.google.com/open?id=1CzfnIY5DZqcu8Vt-zxM3I6sVLnzLhwK5
x = Train.TrainSetArray;
t = Target.TargetSet;
t1=ind2vec(t');
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Fitting Network
hiddenLayerSize = 1;
net = patternnet(hiddenLayerSize,trainFcn);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x',t1);
y = net(x');%%%%% include the test vectors here
perf = perform(net,t,y);
classes = vec2ind(y);
Hi
apologise but its not working im getting
In test (line 14)
Error using bsxfun
Non-singleton dimensions of the two input arrays must match each other.
Error in gsubtract>calc_general (line 40)
c = bsxfun(@minus,a,b);
additionally im trying to achive is confusion matrix across classes mine code is below
%PREPROCESSING
%load data
Tb = readtable('train1.csv','PreserveVariableNames',true);
Tbv = readtable('test1.csv','PreserveVariableNames',true);
%merge both datasets they can be splitted to train target later via dynamic
%parameter
Tbt = union(Tb, Tbv)
%split into train target
TrainSet = Tbt(:,(1:562))%data.simplefitInputs';
TargetSet = Tbt(:,(563:563));%data.simplefitTargets';
TargetSet= table2array(TargetSet)
%make some noise
TrainSetArray = table2array(TrainSet)
noiseSignal = cos(5 * pi * 100 * TrainSetArray)+sqrt(5) * randn(size(TrainSetArray));
%TRAINING
x = TrainSetArray';
%x = noiseSignal';
t = TargetSet';
%t = categorical(Tb(:,(563:563)));FAILING DURING TRAINING
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize,trainFcn);
% Create a Fitting Network
hiddenLayerSize = 1;
net = fitnet(hiddenLayerSize,trainFcn);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t);
%get output
y = net(x);
%get test set
testX = x(:,tr.testInd);
testT = t(:,tr.testInd);
testY = net(testX);
%PLOTTING CONFUSION MATRIXacorss classes as
% https://uk.mathworks.com/help/deeplearning/ref/plotconfusion.html
YPredicted = classify(net.myNet,testX)%Line Causing error SWITCH expression must be a scalar or a character vector.
plotconfusion(testy,YPredicted)

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by