def for_loop_update_col_param_blockwise(self, y_csc, phi_csc, mu0, r, u, c_prev, v_prev):
ncol = y_csc.shape[1]
num_factor = u.shape[1]
prior_Phi = np.diag(np.hstack((self.prior_param['col_bias_scale'] ** -2,
np.tile(self.prior_param['factor_scale'] ** -2, num_factor))))
# Pre-allocate
c = np.zeros(ncol)
v = np.zeros((ncol, num_factor))
# NOTE: The loop through 'j' is completely parallelizable.
for j in range(ncol):
i = y_csc[:, j].indices
nnz_j = len(i)
residual_j = y_csc[:, j].data - mu0 - r[i]
phi_j = phi_csc[:, j].data
u_T = np.hstack((np.ones((nnz_j, 1)), u[i, :]))
post_Phi_j = prior_Phi + \
np.dot(u_T.T,
np.tile(phi_j[:, np.newaxis], (1, 1 + num_factor)) * u_T) # Weighted sum of u_i * u_i.T
post_mean_j = np.squeeze(np.dot(phi_j * residual_j, u_T))
C, lower = scipy.linalg.cho_factor(post_Phi_j)
post_mean_j = scipy.linalg.cho_solve((C, lower), post_mean_j)
# Generate Gaussian, recycling the Cholesky factorization from the posterior mean computation.
cv_j = math.sqrt(1 - self.relaxation ** 2) * scipy.linalg.solve_triangular(C, np.random.randn(len(post_mean_j)),
lower=lower)
cv_j += post_mean_j + self.relaxation * (post_mean_j - np.concatenate(([c_prev[j]], v_prev[j, :])))
c[j] = cv_j[0]
v[j, :] = cv_j[1:]
return c, v
matrix_factorization.py 文件源码
python
阅读 34
收藏 0
点赞 0
评论 0
评论列表
文章目录