Neural Network - MLP

4 次查看(过去 30 天)
Oscar Tsang
Oscar Tsang 2019-12-8
As a challenge for myself I have plotted the classification regions for a neural network classifier(using sigmoid activation functions) for ?1∈[−5,5],?2∈[−5,5]. Which gives the following output:
I was wondering, with the following code I have supplied, is there any possible way to shorten the code and still produce the same output?? I tried as much as I can (still drives me crazy) got it down from 322 to 296 characters without any whitespaces. The matlab code I have provided should work if you copy and paste it into MATLAB. It must work with the version R2018a as I'm too poor to upgrade :).
[x1,x2] = meshgrid(-5:0.01:5, -5:0.01:5);
z1=1./(1+exp(-(-4*x1(:)-x2(:)+1)));
z2=1./(1+exp(-(5*x1(:)-5*x2(:)-2)));
g1= 1./(1+exp(-(-1+5*z2-4)));
g2= 1./(1+exp(-(4*z1-4*z2+1)));
[~, al] = max([g1 g2], [], 2);
figure, hold on, grid on,axis on
plot(x1(al==1),x2(al==1), 'r.');
plot(x1(al==2),x2(al==2), 'k.');
legend('1','2');

回答(1 个)

Thiago Henrique Gomes Lobato
I'm not sure why you want to shorten the code, it is already vectorize and I don't believe it can go so much less than this. Just for fun, here's a very little modified version with 270 characters:
v =-5:0.01:5;
[x1,x2] = meshgrid(v,v);
z1=1./(1+exp((4*x1(:)+x2(:)-1)));
z2=1./(1+exp((5*(x2(:)-x1(:))+2)));
g1= 1./(1+exp((5-5*z2)));
g2= 1./(1+exp((4*z2-4*z1-1)));
[~, al] = max([g1 g2], [], 2);
figure, hold on
plot(x1(al==1),x2(al==1), 'r.');
plot(x1(al==2),x2(al==2), 'k.');
legend('1','2');

类别

Help CenterFile Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by