4-class perceptron classification

Good evening, I hope everyone is well. I have a single-layer perceptron that is meant to take in two inputs and provide an output as one of four classifications. I then need to plot the inputs and the hyperplanes dividing the four classes. I'm getting errors when I try to run the code that I suspect are related to the number of outputs I'm trying to get. Here's the code I'm working with:
if true
% Initialize the Input Space (extended input matrix)
x = [1 1 1 2 2 -1 -2 -1 -2; 1 1 2 -1 0 2 1 -1 -2];
% Initialize the extended target vector
t = [0 1 1 2 2 3 3 4 4];
% Plot the input locations
figure
hold on
for i=1:length(x)
if (t(i)==1)
scatter(x(1,i), x(2,i),'k ', 'filled');
elseif (t(i)==2)
scatter(x(1,i), x(2,i),'r ', 'filled');
elseif (t(i)==3)
scatter(x(1,i), x(2,i),'b ', 'filled');
elseif (t(i)==4)
scatter(x(1,i), x(2,i),'g ', 'filled');
end
end
grid on
line([0 0], ylim, 'linewidth', 1); %y-axis
line(xlim, [0 0], 'linewidth', 1); %x-axis
legend('class_1', 'class_2', 'class_3', 'class_4')
net = perceptron;
net.trainparam.epochs = 100; % Set # of training epochs
net.trainparam.goal = 1e-2; % Set desired max error
net.trainparam.lr = 0.1; % Set desired learning rate
train(net, x, t); % Train the perceptron
predictions = net(x); % Get data predictions
net.IW{:}; % Learned weights
net.b{:}; % Learned biases
% Equation of a line: w1*x1 + w2*x2 + b = 0
% Plot the lines
plot([-net.b{1}/net.IW{:}(1,1),0],[0,-net.b{1}/net.IW{:}(1,2)])
plot([-net.b{2}/net.IW{:}(2,1),0],[0,-net.b{2}/net.IW{:}(2,2)])
hold off;
end
The trainparam epochs, goal and lr are initial conditions an can change but I don't think that's where the problem is.
I'd appreciate any help you're able to provide. Best regards.

4 个评论

use length(t) not length(x)
point t = 0 is not plotted
what are your error messages?
Thanks, Greg. I figured out that I needed to use a second perceptron and two sets of targets to get the required output. The code is working now. Regards, Bill
can you send the modified code
Dang, asking me to go back 6 years .... it took me a minute to find this :)
% Initialize Variables for the Input Spaces (x)
p1 = [1; 1];
p2 = [1; 2];
p3 = [-2; -1];
p4 = [2; 0];
p5 = [-1; 2];
p6 = [-2; 1];
p7 = [-1; -1];
p8 = [-2; -2];
% Concatenate the input vectors into a single input matrix
x = [p1 p2 p3 p4 p5 p6 p7 p8];
% Initialize target vector
t1 = [0 0 0 0 1 1 1 1];
t2 = [0 0 1 1 0 0 1 1];
% Plot the input locations
figure
hold on;
for i=1:length(x)
if t1(i) == 0 && t2(i) == 0
scatter(x(1,i), x(2,i),'k ', 'filled');
elseif t1(i) == 0 && t2(i) == 1
scatter(x(1,i), x(2,i),'r ', 'filled');
elseif t1(i) == 1 && t2(i) == 0
scatter(x(1,i), x(2,i),'b ', 'filled');
else
scatter(x(1,i), x(2,i),'g ', 'filled');
end
end
grid on
line([0 0], ylim, 'linewidth', 1); %y-axis
line(xlim, [0 0], 'linewidth', 1); %x-axis
% Create two perceptrons
net1 = perceptron;
net2 = perceptron;
% Set training parameters
net1.trainparam.epochs = 1000; % Set # of training epochs
net2.trainparam.epochs = 1000;
net1.trainparam.goal = 1e-5; % Set desired max error
net2.trainparam.goal = 1e-5;
net1.trainparam.lr = 0.01; % Set desired learning rate
net2.trainparam.lr = 0.01;
% Train the perceptrons
net1 = train(net1, x, t1);
net2 = train(net2, x, t2);
% Get data predictions
pred1 = net1(x);
pred2 = net2(x);
w_net1 = net1.IW{:}; % net1 learned weights
b_net1 = net1.b{:}; % net1 learned biases
w_net2 = net2.IW{:}; % net2 learned weights
b_net2 = net2.b{:}; % net2 learned biases
% Plot the lines
plot([-b_net1/w_net1(1),0],[0,-b_net1/w_net1(2)], 'k-');
plot([-b_net2/w_net2(1),0],[0,-b_net2/w_net2(2)], 'k--');
hold off;

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by