MATLAB Answers

How to use weights and bias from Neural Network Toolbox for classification

6 views (last 30 days)
David Franco
David Franco on 30 Aug 2020
Answered: David Franco on 1 Oct 2020 at 2:17
I found a code in Matlab Answers for regression:
x = 0:0.1:10; % input dimension 1x101
y = sin(x); % output dimension 1x101
neurons = 10;
net = feedforwardnet(neurons);
net = train(net,x,y);
outputs = sim(net,x);
w1 = net.IW{1,1};
w2 = net.LW{2,1};
b1 = net.b{1};
b2 = net.b{2};
xx = x;
yy = zeros(size(y));
for ii = 1:length(net.inputs{1}.processFcns)
xx = feval(net.inputs{1}.processFcns{ii},...
'apply',xx,net.inputs{1}.processSettings{ii});
end
for jj = 1:size(xx,2)
yy(jj) = w2*tansig(w1*xx(jj) + b1) + b2;
end
for kk = 1:length(net.outputs{2}.processFcns)
yy = feval(net.outputs{2}.processFcns{kk},...
'reverse',yy,net.outputs{2}.processSettings{kk});
end
How can I change the following equation for a classification problem, for example [x,y] = simpleclass_dataset?
yy(jj) = w2*tansig(w1*xx(jj) + b1) + b2;
Because the input and output dimensions are different from the regression example.
For simpleclass_dataset the input dimension is 2x1,000 and the output dimension is 4x1,000.
Thank you!

  0 Comments

Sign in to comment.

Accepted Answer

David Franco
David Franco on 1 Oct 2020 at 2:17
I found the answer (output and yy are equal):
% Simulated a Neural Network
clear
close
clc
rng default
[x,y] = simpleclass_dataset;
neurons = 10;
net = feedforwardnet(neurons);
net = train(net,x,y);
outputs = sim(net,x);
% outputs = round(outputs);
figure, plotconfusion(y,outputs)
w1 = net.IW{1,1};
w2 = net.LW{2,1};
b1 = net.b{1};
b2 = net.b{2};
xx = x;
for ii = 1:length(net.inputs{1}.processFcns)
xx = feval(net.inputs{1}.processFcns{ii},...
'apply',xx,net.inputs{1}.processSettings{ii});
end
a1 = tanh(w1*xx + b1);
yy = purelin(w2*a1 + b2);
for mm = 1:length(net.outputs{1,2}.processFcns)
yy = feval(net.outputs{1,2}.processFcns{mm},...
'reverse',yy,net.outputs{1,2}.processSettings{mm});
end
figure, plotconfusion(y,yy)
=)

  0 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!