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
Greg Heath
2018년 2월 13일
편집: Greg Heath
2018년 2월 13일
use length(t) not length(x)
point t = 0 is not plotted
what are your error messages?
Bill Symolon
2018년 2월 13일
Akash
2024년 5월 19일
can you send the modified code
Bill Symolon
2024년 5월 20일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Define Shallow Neural Network Architectures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!