Python Numpy Matrix更新混淆

时间:2016-04-05 09:28:15

标签: numpy matrix sparse-matrix matrix-factorization

这是我的矩阵分解代码的一部分(一个非常奇怪的nmf版本)。我的问题是,虽然每次迭代时,我都会保存W和H矩阵的旧副本,当我在W完成每次更新后比较old_W和W时,它们实际上是相同的!因此,实际错误输出始终为0,而​​while循环在第一次迭代后停止。但是,“#print old - new”表示元素W [r] [i]实际上每次都更新。我没看到什么?

def csmf(V, l, max_iter, err, alpha=0.01, beta=0.01, lamb=0.01):
  W = np.random.rand(V.shape[0], l)
  H = np.random.rand(l, V.shape[1])
  n = V.shape[0]
  N = V.shape[1]

  NwOone = 60
  NwOtwo = 60
  NhOone = 50
  NhOtwo = 50

  for t in range(max_iter):
    old_W = W # save old values
    old_H = H
    old = criterion(V,old_W,old_H,l,alpha,beta,lamb)
    print "iteration ", t

    ##### update W
    print "updating W"
    setw = range(0,n)
    subset_one = random.sample(setw,NwOone)
    subset_two = calcGw(V, W, H, n, l, alpha, beta, NwOtwo)
    chosen = np.intersect1d(subset_one,subset_two)

    for r in chosen:
      for i in range(len(W[0])):
        update = wPosNeg(W[r],N,i,l,V,r,beta,H)
        old = W[r][i]
        W[r][i] = update
        new = W[r][i]
        #print old - new

    ##### update H
    print "updating H"
    seth = range(0,N)
    subset_oneh = random.sample(seth,NhOone)
    subset_twoh = calcGh(V, W, H, N, l, NhOtwo,lamb)
    chosenh = np.intersect1d(subset_oneh,subset_twoh)

    for s in chosenh: # column
      for i in range(len(H)):
        updateh = hPosNeg(H[i],n,i,l,V,s,lamb,W)
        H[i][s] = updateh

    ##### check err
    print "Checking criterion"
    print criterion(V,W,H,l,alpha,beta,lamb)
    print criterion(V,old_W,old_H,l,alpha,beta,lamb)
    actual = abs(criterion(V,W,H,l,alpha,beta,lamb)  -criterion(V,old_W,old_H,l,alpha,beta,lamb))
    if actual <= err: return W, H, actual
  return W, H, actual

dmat = np.random.rand(100,80)
W, H, err = csmf(dmat, 1, 10, 0.001, alpha=0.001, beta=0.001, lamb=0.001)
print err

1 个答案:

答案 0 :(得分:0)

在这些方面: old_W = W # save old values old_H = H 你没有保存副本,你保留一个参考(old_W和W是同一块内存)。

试试这个: old_W = W.copy() # save old values old_H = H.copy()