How to desig MLP neural network in Matlab?

杀马特。学长 韩版系。学妹 提交于 2021-02-19 08:22:11

问题


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 .

Here's the problem :

enter image description here

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.

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


回答1:


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

% Network setup

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';

% Training

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

% We are making the surface to take a contour:

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

% Calculate values from trained net for whole grid(we will see how two data types were separated)

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

% Plotting and showing the contour

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

The result Figure1 and Figure2



来源:https://stackoverflow.com/questions/20190471/how-to-desig-mlp-neural-network-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!