MATLAB实现智能计算方法课程所有实验代码资源链接为:MATLAB实现智能计算方法课程所有实验代码资源
MATLAB实现智能计算方法课程所有实验汇总博客链接为:MATLAB实现智能计算方法实验汇总
异或问题本质上是一个二分类问题:输入为向量[x1,x2],输出是标量,取值限定为0或1。
序号 | x1 | x2 | D期望输出 |
---|---|---|---|
1 | 0 | 0 | 0 |
2 | 0 | 1 | 1 |
3 | 1 | 0 | 1 |
4 | 1 | 1 | 0 |
设计网络结构(以图的形式放入报告);
手工编码(python或MATLAB),实现对异或问题的分类计算。
用MATLAB工具箱编程实现模型仿真。
测试数据1:x1=0.95,x2=0.03。
测试数据2:x1=0.92,x2=0.89。
给定一系列已知的采样点,就可以近似确定在某未知自变量位置处的函数值(18个点)
X=-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9
Y=-129,-32,-118,-138,-125,-97,-55,-23,-4,2,1,-31,-72,-121,-142,-174,-155,-77
手工编码(python或MATLAB),实现曲线拟合。
用MATLAB工具箱编程实现曲线拟合,改变spread,观察拟合结果并截图放入报告中。
设计具有一层隐含层,隐含层为4个神经单元,输入层有2个神经单元,输出层有1个神经单元的RBF神经网络,网络结构如下图1。
clc;
clear;
P = [0, 0, 1, 1; 0, 1, 0, 1];
T = [0, 1, 1, 0];
t = [0 1; 0 1];
z = dist(P', t);
G = radbas(z);
G =[G, ones(4,1)];
%G.'*G的逆矩阵*G.'
w = G'*G\G'*T';
Y = G*w;
disp('训练输出');
disp(Y');
P1 = [0.95 0.92; 0.03 0.89];
z1 = dist(P1', t);
G1 = radbas(z1);
G1 = [G1, ones(2,1)];
T1 = G1*w;
disp('测试输出');
disp(T1');
执行程序,输出结果如下图2。
应用MATLAB工具箱编程实现模型仿真并用测试数据进行预测,程序代码如下:
clc
clear
P = [0, 0, 1, 1; 0, 1, 0, 1];
T = [0, 1, 1, 0];
%创建正规化网络
net = newrbe(P, T);
out = sim(net, P);
disp('训练输出');
disp(out);
P1 = [0.95 0.92; 0.03 0.89];
out1 = sim(net, P1);
disp('测试输出')
disp(out1);
执行程序,输出结果如下图3。
clc;
clear;
M = 10;
centers = [];
deltas = [];
set = {};
gap = 0.01;
X = -9: 1: 8;
P = length(X);
Y = [-129 -32 -118 -138 -125 -97 -55 -23 -4 2 1 -31 -72 -121 -142 -174 -155 -77];
A = randperm(M);
for i = 1: M
centers(i) = X(A(i));
end
done = 0;
while(~done)
for i = 1: M
set{i} = [];
end
for i = 1: P
distance = 100;
for j = 1: M
curr = abs(X(i) - centers(j));
if curr < distance
sets = j;
distance = curr;
end
end
set{sets} = [set{sets}, X(i)];
end
for i = 1: M
new_centers(i) = sum(set{i})/length(set{i});
sum1(i) = abs(centers(i) - new_centers(i));
end
if sum(sum1) > gap
done = 0;
centers = new_centers;
else
done = 1;
end
end
for i = 1: M
curr = abs(centers - centers(i));
[curr_2, b] = min(curr);
curr(b) = 100;
curr_2 = min(curr);
deltas(i) = 2*curr_2;
end
for i = 1: P
for j = 1: M
curr = abs(X(i) - centers(j));
K(i, j) = exp(-curr^2/(2*deltas(j)^2) );
end
end
weights = K'*K\K'*Y';
X1 = -9: .1: 8;
for i = 1: length(X1)
sum = 0;
for j = 1:M
curr = weights(j)*exp(-abs(X1(i) - centers(j))^2/(2*deltas(j)^2));
sum = sum + curr;
end
Y1(i)=sum;
end
figure;
scatter(X, Y, 'o');
hold on;
plot(X1, Y1, 'b-')
拟合结果如下。
应用MATLAB工具箱编程实现模型仿真并用测试数据进行预测,程序代码如下,并设置不同spread,得到不同的拟合结果。
clc;
clear;
P = -9: 1: 8;
T = [-129,-32,-118,-138,-125,-97,-55,-23,-4,2,1,-31,-72,-121,-142,-174,-155,-77];
spread = 0.5;
tic;
net = newrbe(P, T, spread);
test = -9: .1: 8;
out = sim(net, test);
toc;
figure;
plot(P, T, 'o')
hold on;
plot(test, out, 'b-');
legend('输入的数据', '拟合的函数');
观察不同spread运行的结果,可以发现,spread越大,拟合的函数越平滑,但效果也越差,反之,spread越小,平滑度越差,但离群点会变少,然而spread太小,拟合的函数效果也会变差。综合上述三幅图,发现spread为1时效果最好。