4-class perceptron classification
9 views (last 30 days)
Show older comments
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 Comments
Answers (0)
See Also
Categories
Find more on Define Shallow Neural Network Architectures 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!