在Matlab中创建径向基函数内核矩阵 [英] Creating a Radial basis function kernel matrix in matlab
问题描述
我从未使用过matlab,并且我有关于局部化敏感区域函数的代码一个>.
I never used matlab, and I have this code about kernalized locality sensitive functions.
我认为以下代码正在尝试创建 RBF内核函数:
I think that the following code is trying to create the kernalized matrix of a RBF kernel function:
%demo script for KLSH
X = load('iris.mtx');
...
[n,d] = size(X);
%form RBF over the data:
nms = sum(X'.^2);
K = exp(-nms'*ones(1,n) -ones(n,1)*nms + 2*X*X');
您可以在
现在,我找不到K
(内核矩阵)的计算方式与内核函数公式的相关性:
Now, I cannot find the correlation of how K
(the kernel matrix) is computed and the kernel function formula:
请问您能帮我弄清楚如何创建K
(并向我解释上面的代码)吗?
Can you help me to figure out how K
is created (and explain me the code above) please?
推荐答案
整个技巧都是基于以下事实:您要计算矩阵K_ij = K(x_i,x_j)= f(|| x_i-x_j || ^ 2)以有效的方式.矩阵计算基于点积,即乘法,而不是基于差的范数.如果您不想使用循环(并且不想使用Matlab或R等语言),则必须弄清楚如何使用矩阵运算来表达|| x_i-x_j || ^ 2,因此:
The whole trick is based on the fact that you want to compute matrix K_ij = K(x_i, x_j) = f(||x_i - x_j||^2) in an efficient manner. Matrix computations are based on dot products, thus multiplications, not on norm of a difference. If you do not want to use loops (and in languages like matlab or R you do not want to) you have to figure out how to express this ||x_i - x_j||^2 using matrix operations, thus:
||x_i - x_j||^2 = <x_i - x_j, x_i - x_j>
= <x_i, x_i> - <x_i, x_j> - <x_j, x_i> + <x_j, x_j>
= ||x_i||^2 - 2<x_i, x_j> + ||x_j||^2
这正是所实现的
首先,它们取您数据的平方,因为|| x_i || ^ 2 = SUM_a x_i_a ^ 2
First they take square of your data, as ||x_i||^2 = SUM_a x_i_a^2
nms = sum(X'.^2);
接下来,他们使用与1的向量相乘来计算求和运算的结果
next they use multiplication with vector of ones to compute the sum opertion getting
nms'*ones(1,n)
是|| x_i || ^ 2的向量,并且类似地|| x_j || ^ 2的向量是
which is vector of ||x_i||^2's, and analogously vector of ||x_j||^2's is
ones(n,1)*nms
最后,它们使用我之前写的分解进行合成,因此
and finally they compose using decomposition I wrote before, thus
-nms'*ones(1,n) -ones(n,1)*nms + 2*X*X')
只是一个矩阵A_ij =-|| x_i-x_j || ^ 2
is just a matrix A_ij = - ||x_i - x_j ||^2
在您的情况下,您希望除以2sigma ^ 2,因此在将之前的位置放在方括号中后,只需将其放在exp下即可,例如
In your case, you want to have division by 2sigma^2, thus just put it under the exp, after taking previous arugment in brackets, like
Ks = exp(-(nms'*ones(1,n) -ones(n,1)*nms + 2*X*X')/(2*sigma^2));
这篇关于在Matlab中创建径向基函数内核矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!