在MATLAB中绘制除数MLP线和图表 [英] Draw divisory MLP line together with chart in MATLAB

查看:200
本文介绍了在MATLAB中绘制除数MLP线和图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要画出等分线和下图:

I need to plot the divisory line together with the graph below:

我用来训练MLP神经网络的代码在这里:

The code I used to train the MLP neural network is here:

circles = [1 1; 2 1; 2 2; 2 3; 2 4; 3 2; 3 3; 4 1; 4 2; 4 3];
crosses = [1 2; 1 3; 1 4; 2 5; 3 4; 3 5; 4 4; 5 1; 5 2; 5 3];

net = feedforwardnet(3);
net = train(net, circles, crosses);

plot(circles(:, 1), circles(:, 2), 'ro');
hold on
plot(crosses(:, 1), crosses(:, 2), 'b+');
hold off;

但是我也想在图表中显示分隔各组的线.我该如何进行?预先感谢.

But I'd like to show the line separating the groups in the chart too. How do I proceed? Thanks in advance.

推荐答案

首先,您没有正确地训练神经网络.您必须同时使用circlescrosses作为神经网络的输入样本,并且输出必须是两个神经元的输出,其中[1 0]作为输出将表示circle类是分类的对象be和[0 1]是十字架的分类.

First off, you're not training your neural network properly. You'd have to use both circles and crosses as input samples into your neural network and the output will have to be a two neuron output where [1 0] as the output would denote that the circles class is what the classification should be and [0 1] is what the crosses classification would be.

此外,每个是一个输入样本,而每一行都是一个要素.因此,您必须对这两者进行转置并制作更大的输入矩阵.您还需要根据我们刚才所说的内容来制作输出标签:

In addition, each column is an input sample while each row is a feature. Therefore, you have to transpose both of these and make a larger input matrix. You'll also need to make your output labels in accordance with what we just talked about:

X = [circles.' crosses.'];
Y = [[ones(1, size(circles,1)); zeros(1, size(circles,1))] ...
     [zeros(1, size(crosses,1)); ones(1, size(crosses,1))]];

现在训练您的网络:

net = feedforwardnet(3);
net = train(net, X, Y);

现在,如果您想弄清楚每个点属于哪个类别,您只需获取最大的神经元输出,而无论哪个给您的神经元输出最多,这就是它所属的类别.

Now, if you want to figure out which class each point belongs to, you simply take the largest neuron output and whichever one gave you the largest, that's the class it belongs to.

现在,要回答您的实际问题,如果使用MATLAB工具箱,则没有直接的方法来显示神经网络的分离线.但是,您可以显示分隔的区域,并且可以添加一些透明度,以便可以将其覆盖在图形上方.

Now, to answer your actual question, there's no direct way to show the "lines" of separation with Neural Networks if you use the MATLAB toolbox. However, you can show regions of separation and maybe throw in some transparency so that you can overlay this on top of the figure.

要执行此操作,请定义一个跨两个类但具有更细颗粒的2D坐标网格,例如0.01.通过神经网络运行此程序,查看最大输出神经元是多少,然后在您的图形上相应地进行标记.

To do this, define a 2D grid of coordinates that span your two classes but with a finer grain... say... 0.01. Run this through the neural network, see what the maximum output neuron is, then mark this accordingly on your figure.

想到这样的事情:

%// Generate test data
[ptX,ptY] = meshgrid(1:0.01:5, 1:0.01:5);
Xtest = [ptX(:).'; ptY(:).'];

%// See what the output labels are
out = sim(net, Xtest);
[~,classes] = max(out,[],1);

%// Now plot the regions
figure;
hold on;
%// Plot the first class region
plot(Xtest(1, classes == 1), Xtest(2, classes == 1), 'y.');
%// Add transparency
alpha(0.1);

%// Plot the second class region
plot(Xtest(1, classes == 2), Xtest(2, classes == 2), 'g.');
%// Add transparency
alpha(0.1);

%// Now add the points
plot(circles(:, 1), circles(:, 2), 'ro');
plot(crosses(:, 1), crosses(:, 2), 'b+');

前两行代码生成一堆测试(x,y)点,并确保它们位于2行输入矩阵中,这正是网络输入所需要的.我使用 meshgrid 来生成这些点.接下来,我们使用 sim 模拟或将输入放入神经网络.完成此操作后,每个输入点将有两个输出神经元神经网络响应,我们将查看哪个输出神经元给了我们最大的响应.如果第一个输出给了我们最大的响应,我们认为该输入属于第一类.如果没有,那就是第二堂课.通过使用 max 并独立查看每列来简化此操作-每个输入样本一列,查看哪个位置为我们提供了最大的数量.

The first two lines of code generate a bunch of test (x,y) points and ensures that they're in a 2 row input matrix as that is what the network inputs require. I use meshgrid for generating these points. Next, we use sim to simulate or put in inputs into the neural network. Once we do this, we will have two output neuron neural network responses per input point where we take a look at which output neuron gave us the largest response. If the first output gave us the largest response, we consider the input as belonging to the first class. If not, then it's the second class. This is facilitated by using max and looking at each column independently - one column per input sample and seeing which location gave us the maximum.

完成此操作后,我们将创建一个新图形,并绘制属于第1类的点(黄色为圆形)和作为绿色的第二类(十字).我添加了一些透明度,以确保我们可以看到带有点的区域.之后,我使用您的代码按常规绘制点.

Once we do this, we create a new figure and plot the points that belonged to class 1, which is the circles, in yellow and the second class, which is the crosses, in green. I throw in some transparency to make sure we can see the regions with the points. After, I plot the points as normal using your code.

使用上面的代码,我得到了这个图:

With the above code, I get this figure:

如您所见,您的模型存在一些分类错误.具体来说,有三个十字将被误分类为圆形.您将不得不在隐藏层中使用大量的神经元,并且可能需要使用其他激活功能,但这肯定足以使您入门.

As you can see, your model has some classification inaccuracies. Specifically, there are three crosses that would be misclassified as circles. You'll have to play around with number of neurons in the hidden layer as well as perhaps using a different activation function but this certainly is enough to get you started.

祝你好运!

这篇关于在MATLAB中绘制除数MLP线和图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆