1

这是我的矩阵分解代码的一部分(一个非常奇怪的 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
4

1 回答 1

0

在这些行中: old_W = W # save old values old_H = H 您不是在保存副本,而是在保留引用(old_W 和 W 是同一块内存)。

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

于 2016-04-05T17:31:57.433 回答