Scipy Python中的稀疏基质计算



我正在尝试实现本文中理查森·卢西·卷曲卷积算法中所述的停止标准。我的测试图像具有2.2 MPX。我需要计算:

estimator = numpy.dot(P_e_ortho, im_deconv.flatten())

其中

im_deconv = [[]] # Image L channel as a 2D array
m = 2.2E6 # im_deconv.size
P_e_ortho = scipy.sparse.identity(m, dtype='int8') - 
            1/m * np.ones((m, m), dtype='int8')

基本上,P_e_ortho在对角线上具有1 - 1/m,而- 1/m则在其他地方。

现在,此代码返回记忆错误(需要4.8×10¹图单元格(。我如何避免在计算中处理整个方形矩阵?

scipy.sparse.identity(m, dtype='int8') * (1 - 1/m)

可以很好地设置对角线,但是如何更改非对角性元素?

找到解决方案:

# Construct a Row-based linked list sparse matrix
P_e_ortho = scipy.sparse.lil_matrix((m, m)) 
P_e_ortho[:, :] = -1/m # Sets all elements
P_e_ortho.setdiag(1-1/m) # Sets the diagonal
P_e_ortho = P_e_ortho.tocsr() # Convert to Compressed Sparse Row matrix
estimator = P_e_ortho.dot(im_deconv.flatten()) # Compute the dot product

最新更新