Autoassociation
autoassociation是一个很有趣的可以自己计算权重的算法。
假设输入向量为X,输出向量为Y;权重由向量W来表示,那么autoassociation的计算过程为:
①计算 Wx = W * X;
②计算 error = X - Wx;
③计算 ΔW = η*error*xT
④计算 Wnew = W + ΔW
截取PPT中的介绍:
PPT中给出的例子
PPT里面给的例子是X=Y的情况,在这种情况下,该算法的matlab实现是:
function [weight,f]=heterassociation(x,times,n) w = zeros(1,n); step = 0.1; wx = w.*x; for i = 1: times error = x - wx; delta_w = step * error * x'; w = w + delta_w; wx = w.*x; end weight = w; f = w * x';
参数x是输入向量,times是计算次数,n表示的是x向量的维数。
我们可以试一下一个1*10的随机矩阵,看看使用autoassociation计算出来的结果的相似程度,以及当时的权重值是多少。
>> x=rand(10,1)
x =
0.853031117721894
0.622055131485066
0.350952380892271
0.513249539867053
0.401808033751942
0.075966691690842
0.239916153553658
0.123318934835166
0.183907788282417
0.239952525664903
现在使用autoassociation函数,计算它的输出值:
>> [weight,output]=autoassociation(x,100,10)
output =
0.853031116343490
0.622055130479894
0.350952380325171
0.513249539037699
0.401808033102665
0.075966691568088
0.239916153165980
0.123318934635896
0.183907787985242
0.239952525277166
我们保留4位小数,看一下此时的权重的计算结果是多少(权重初始化时为zeros(1,n)):
vpa(weight,4)
ans =
[ 0.3971, 0.2896, 0.1634, 0.2389, 0.187, 0.03536, 0.1117, 0.0574, 0.0856, 0.1117]
[ 0.2896, 0.2112, 0.1191, 0.1742, 0.1364, 0.02579, 0.08144, 0.04186, 0.06243, 0.08145]
[ 0.1634, 0.1191, 0.06721, 0.09829, 0.07695, 0.01455, 0.04595, 0.02362, 0.03522, 0.04595]
[ 0.2389, 0.1742, 0.09829, 0.1437, 0.1125, 0.02128, 0.06719, 0.03454, 0.05151, 0.0672]
[ 0.187, 0.1364, 0.07695, 0.1125, 0.0881, 0.01666, 0.0526, 0.02704, 0.04032, 0.05261]
[ 0.03536, 0.02579, 0.01455, 0.02128, 0.01666, 0.003149, 0.009945, 0.005112, 0.007624, 0.009947]
[ 0.1117, 0.08144, 0.04595, 0.06719, 0.0526, 0.009945, 0.03141, 0.01614, 0.02408, 0.03141]
[ 0.0574, 0.04186, 0.02362, 0.03454, 0.02704, 0.005112, 0.01614, 0.008298, 0.01238, 0.01615]
[ 0.0856, 0.06243, 0.03522, 0.05151, 0.04032, 0.007624, 0.02408, 0.01238, 0.01846, 0.02408]
[ 0.1117, 0.08145, 0.04595, 0.0672, 0.05261, 0.009947, 0.03141, 0.01615, 0.02408, 0.03142]
假如输入不等于输出,即X=>Y,且X≠Y,则程序改为:
function [weight,f] = autoassociation2(x,y,times,n) w=zeros(n); step = 0.1; wx = w*x; for i = 1:times error = y - wx; delta_w = step * error*x'; w = w + delta_w; wx = w*x; end weight = w; f = wx;
现在,我们来试试这种情况下的权重的计算效果:
x与刚才不变,我们来建立一个随机的向量y:
>> y=rand(10,1)
y =
0.780252068321138
0.389738836961253
0.241691285913833
0.403912145588115
0.096454525168389
0.131973292606335
0.942050590775485
0.956134540229802
0.575208595078466
0.059779542947156
调用autoassociation2函数,计算新的权重向量:
>> [weight,output]=autoassociation2(x,y,100,10)
>> wei2 = vpa(weight,4)
wei2 =
[ 0.3632, 0.2648, 0.1494, 0.2185, 0.1711, 0.03234, 0.1021, 0.0525, 0.0783, 0.1022]
[ 0.1814, 0.1323, 0.07464, 0.1092, 0.08545, 0.01616, 0.05102, 0.02623, 0.03911, 0.05103]
[ 0.1125, 0.08204, 0.04629, 0.06769, 0.05299, 0.01002, 0.03164, 0.01626, 0.02425, 0.03165]
[ 0.188, 0.1371, 0.07735, 0.1131, 0.08856, 0.01674, 0.05288, 0.02718, 0.04053, 0.05289]
[ 0.0449, 0.03274, 0.01847, 0.02701, 0.02115, 0.003998, 0.01263, 0.006491, 0.00968, 0.01263]
[ 0.06143, 0.0448, 0.02527, 0.03696, 0.02894, 0.005471, 0.01728, 0.008881, 0.01324, 0.01728]
[ 0.4385, 0.3198, 0.1804, 0.2638, 0.2066, 0.03905, 0.1233, 0.06339, 0.09454, 0.1233]
[ 0.4451, 0.3245, 0.1831, 0.2678, 0.2096, 0.03963, 0.1252, 0.06434, 0.09595, 0.1252]
[ 0.2677, 0.1952, 0.1102, 0.1611, 0.1261, 0.02384, 0.0753, 0.03871, 0.05772, 0.07532]
[ 0.02783, 0.02029, 0.01145, 0.01674, 0.01311, 0.002478, 0.007826, 0.004023, 0.005999, 0.007827]
我们用新的权重向量乘以输入向量X,得到:
>> wei2*x
ans =
0.78025206706038035435231538876711
0.38973883633138070734800454998065
0.24169128552326829648772387731088
0.40391214493521717748970228528981
0.096454525012533578708031238589194
0.13197329239315730614229805625988
0.94205058925325084392983215667686
0.95613453868470610224455203956065
0.57520859414884153265933234713389
0.059779542850546555876784678932704
可以看到使用训练后的权重向量计算出的结果已经和期待的输出Y向量很接近了。
所以,在输入向量维度为d,输出向量的维度也为d的情况下,可以使用autoassociation算法来计算输入与输出之间的权重向量。