非负矩阵分解的C语言代码实现
现在在研究非负矩阵分解的问题,不知道非负矩阵分解C语言代码怎么弄,求一下NMF(非负矩阵分解算法)的实现代码,谢谢了。
下面是matlab程序:
function [W H]=NMF( V, rdim )
%
% Check that we have non-negative data
if min(V(:))<0, error('Negative values in data!'); end
% Globally rescale data to avoid potential overflow/underflow
%V = V/max(V(:));
% Dimensions
vdim = size(V,1);
samples = size(V,2);
[S V1 D]=svds(V,rdim);
% Create initial matrices
W = abs(randn(vdim,rdim));
H = abs(randn(rdim,samples));
% Calculate initial objective
objhistory = sum(sum((V.*log(V./(W*H))) - V + W*H));
timestarted = clock;
% Start iteration
iter = 0;
while 1,
iter = iter+1;
% Save old values
Wold = W;
Hold = H;
% Compute new W and H (Lee and Seung; NIPS*2000)
W = W.*((V./(W*H + 1e-6))*H')./(ones(vdim,1)*sum(H'));
H = H.*(W'*(V./(W*H + 1e-6)))./(sum(W)'*ones(1,samples));
% Renormalize so rows of H have constant energy
norms = sqrt(sum(H'.^2));
H = H./(norms'*ones(1,samples));
W = W.*(ones(vdim,1)*norms);
% Calculate objective
newobj = sum(sum((V.*log(V./(W*H))) - V + W*H));
objhistory = [objhistory newobj];
if (newobj<1e-6 )
break;
end
end