非负矩阵分解

NMF(非负矩阵分解)
\(\frac{1}{2}*|X-WH|^2+\alpha*l_1*|W|+\alpha*l_1*|H|+\alpha*(1-l_1)*W^2+\alpha*(1-l_1)*H^2\)
输入 :X,\(\alpha,l_1,p\)
输出 :W,H
1.计算W,H的\(l_1,l_2\)惩罚系数,样本个数n_samples,因子个数n_components,并令i=0,Ht=H'
2.更新W,更新H,i=i+1;回到2

更新W规则:XH'=WHH' W=XH'*inv(HH') 
def updata_W(X,W,Ht,l1_W,l2_W,weights):
    n_samples=X.shape[0] #n
    n_components = X.shape[1] #m
    HHt = np.dot(Ht.T,Ht)#pxp
    HHt.flat[::n_components+1]+=l2_W
    grad =np.dot(np.multiply(weights, np.dot(W,Ht.T)),Ht)-np.dot(np.multiply(weights, X),Ht)#nxp
    HESS = np.ones((grad.shape[0],grad.shape[1]))
    for i in range(grad.shape[1]):
        HESS[:,i] *=HHt[i,i]
    WW = np.maximum(W-grad/HESS,0)
    for i in range(grad.shape[0]):
        for j in range(grad.shape[1]):
            W[i,j]=WW[i,j]
    violation = sum(sum(grad ))
    return violation

更新H:X'W=H'W'W; H' = X'W*inv(W'W) 

def updata_H(X,W,Ht,l1_H,l2_H,weights):
    n_samples=X.shape[0] #n
    n_components = X.shape[1] #m
    HHt = np.dot(W.T,W)#pxp
    HHt.flat[::n_components+1]+=l2_H
    grad = np.dot(np.multiply(weights.T,np.dot(Ht,W.T)),W)-np.dot(np.multiply(weights.T,X.T),W)#mxp
    HESS=np.ones((grad.shape[0],grad.shape[1]))
    for i in range(grad.shape[1]):
        HESS[:, i] *= HHt[i, i]
    HH = np.maximum(Ht - grad / HESS, 0)
    for i in range(grad.shape[0]):
        for j in range(grad.shape[1]):
            Ht[i, j] = HH[i, j]
    violation = sum(sum(grad ))
    return violation
l1_W=l1_H=l1_ratio*alpha;
l2_W=l2_H=(1-l1_ratio)*alpha;
posted @ 2018-01-15 16:22  blog_hfg  阅读(107)  评论(0)    收藏  举报