如何在Matlab中设计MLP神经网络? [英] How to desig MLP neural network in Matlab?

查看:239
本文介绍了如何在Matlab中设计MLP神经网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用三层神经网络设计了XOR.现在,我有一个类似于xor的新问题,但仍然不知道如何解决.

Hi I've design the XOR with a three layered Neural Network. Now I have a new problem similar to xor but still I can't figure out how to solve it .

这是问题所在:

我想将红色区域与蓝色区域区分开.我可以将垂直区域设置为-1到1,水平区域-1到1.

I want to distinguish the red area from blue area.As you can I have an area of -1 to 1 vertically and -1 to 1 horizontally.

有人能给我一个线索吗?还是matlab中的某种示例代码或网络配置?

can any body give me a clue? or some sort of sample code or network configuration in matlab?

推荐答案

当我学习ANN概念时,我也完成了类似的任务,我将与您共享所需的代码,这些更改只需很少的更改即可达到目标.

I had a similar task when I was learning about the ANN concept, I will share the code with you that requires minimal changes to reach the goal.

clear all
close all
K1size = 200;
K2size = 300;
K1 = randn(K1size,2) - [ones(K1size,1)*2 ones(K1size,1)];
K2 = randn(K2size,2) + [ones(K2size,1) ones(K2size,1)*2];
figure(1)
plot(K1(1, 1), K1(1, 2), 'ro');
hold on
for i = 1:200
    plot(K1(i, 1), K1(i, 2), 'ro');
end;
for i = 1:300
    plot(K2(i, 1), K2(i, 2), 'bx');
end;
xlim([-5 5]);
ylim([-5 5]);
hold off
input = [K1 ;K2];
target = [zeros(K1size,1); ones(K2size,1)]; %K1 data gets target values of zero, K2 - ones

网络设置

net = fitnet(5);
net.trainParam.min_grad = 0.000001;
net.trainParam.epochs = 200;
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net.trainParam.max_fail = 15;
net.layers{1}.transferFcn = 'logsig';

培训

net = train(net, inputN', targetN');
yN = net(inputN');

我们正在使表面具有轮廓:

n = 50;
xx = linspace(-20, 20, n);
yy = linspace(-20, 20, n);
[X, Y] = meshgrid(xx, yy);
Z = zeros(n, n);

% 从训练好的网络计算整个网格的值(我们将看到两种数据类型是如何分离的)

    G =  net( [Y(:)' ; X(:)'] ) ; %  0 <= G <=1, like targets, so we can use it to make surface
    Z = vec2mat(G, n);

绘制并显示轮廓

figure(2)
surf(X, Y, Z);
figure(1)
hold on
contour(X,Y,Z,1, 'linewidth',4)

结果

这篇关于如何在Matlab中设计MLP神经网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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