- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当尝试从下面求解大型稀疏线性方程组时,我只是得到一个 MemoryError:
。我该如何解决这个问题?
此外,此代码基于 Matlab 中的实现,应该可以正常运行。原版中,M
是一个三维矩阵,不知道是否是因为我修改将M
转换为二维而出现内存问题scipy.sparse.lil_matrix
(而不是coo
)我可以迭代地填充M
。
def dectrans(features, faces, template):
"""
Decode transformation matrix.
features : shape (3*N) numpy.ndarray
faces : (Nx3) array
template : (Nx3) array
"""
ftrs = features
weight = 1
fixvertex = 1
fixto = np.zeros((3))
# M shape originally (len(template), len(template), 10 * len(template))
M = scipy.sparse.lil_matrix((len(template)+fixvertex, len(template) * 10 * len(template)))
dx = scipy.sparse.lil_matrix((len(template)+fixvertex,3))
# build laplacian system
for i in range(len(faces)):
v = faces[i,:]
...
M[v][:,v] = M[v][:,v] + WIJ # WIJ some 3x3 matrix
dx[v,:] = dx[v,:] + WIJ.dot(x.T)
weight = np.ones((fixvertex)) * weight
for i in range(fixvertex):
M[len(template)+i, fixvertex-1] = weight[i]
dx[len(template):len(template),:] = fixto.dot(np.tile(weight, (3)))
M = np.real(M)
dx = np.real(dx)
Mt = M.T
model = scipy.sparse.linalg.spsolve(Mt @ M, Mt.dot(dx)) # here I get the error
return model
这是我得到的错误的回溯:
MemoryError Traceback (most recent call last)
<ipython-input-10-9aa6e73eb179> in <module>
20 rr = encrelrot(v_positions, faces, r_v_positions, f_neighbors)
21
---> 22 modelout = dectrans(decrelrot(rr, f_neighbors), faces, r_v_positions)
<ipython-input-8-cdb51dd3cadf> in dectrans(features, faces, template)
616 print("Size dx", dx.nnz)
617 #M = M.tocsr()
--> 618 model = scipy.sparse.linalg.spsolve(Mt @ M, Mt.dot(dx))
619
620 return model
~/anaconda3/lib/python3.6/site-packages/scipy/sparse/base.py in __matmul__(self, other)
560 raise ValueError("Scalar operands are not allowed, "
561 "use '*' instead")
--> 562 return self.__mul__(other)
563
564 def __rmatmul__(self, other):
~/anaconda3/lib/python3.6/site-packages/scipy/sparse/base.py in __mul__(self, other)
480 if self.shape[1] != other.shape[0]:
481 raise ValueError('dimension mismatch')
--> 482 return self._mul_sparse_matrix(other)
483
484 # If it's a list or whatever, treat it like a matrix
~/anaconda3/lib/python3.6/site-packages/scipy/sparse/compressed.py in _mul_sparse_matrix(self, other)
494 other.indptr, other.indices),
495 maxval=M*N)
--> 496 indptr = np.empty(major_axis + 1, dtype=idx_dtype)
497
498 fn = getattr(_sparsetools, self.format + '_matmat_pass1')
MemoryError:
最佳答案
回溯应显示问题是否出现在 spsolve
中,还是在创建一个或两个参数 Mt@M
或 Mt.dot(dx )
。
具有 M
和 dx
形状 ((6891, 474721000)
, (6891, 3)
Mt@M
(474721000,6891) + (6891, 474721000) => (474721000, 474721000)
Mt.dot(dx) # why not Mt@dx?
(474721000,6891) + (6891, 3) => (474721000, 3)
根据这些非零值的结构,@ 乘积可能比 M
具有更多的非零值,这可能会产生内存错误。其中一个的回溯可能会帮助我们诊断这一点。
更常见的内存错误是由于尝试从稀疏数组创建密集数组而导致的,但这里似乎确实是这种情况。但同样,回溯可以帮助排除这种情况。
如果增量填充矩阵值,则推荐使用lil
格式。 csr
用于矩阵乘积,但如果需要,sparse
可以轻松将 lil
转换为 csr
。所以这不应该是一个问题。
===
创建一个包含 1 个非零元素的稀疏矩阵:
In [263]: M=sparse.lil_matrix((1000,100000))
In [264]: M
Out[264]:
<1000x100000 sparse matrix of type '<class 'numpy.float64'>'
with 0 stored elements in LInked List format>
In [265]: M[0,0]=1
In [266]: M
Out[266]:
<1000x100000 sparse matrix of type '<class 'numpy.float64'>'
with 1 stored elements in LInked List format>
这个@
没有产生内存错误,并且结果只有1个非零项,正如预期的那样。但运行时有明显的延迟,表明它正在进行一些大型计算:
In [267]: M.T@M
Out[267]:
<100000x100000 sparse matrix of type '<class 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>
在 csr
上执行相同的 @
等效操作不会产生时间延迟:
In [268]: M1=M.tocsr()
In [269]: M1.T@M1
Out[269]:
<100000x100000 sparse matrix of type '<class 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Column format>
===
您提到 MATLAB 中的 3d 稀疏矩阵。您必须使用某种第 3 方扩展或解决方法,因为 MATLAB 稀疏仅限于 2d(至少是几年前我在 FEM 工作中使用它时)。
scipy.sparse
csc
格式类似于 MATLAB 的内部稀疏。事实上,如果您通过 save
和 scipy.io.loadmat
传输矩阵,就会得到这样的结果。 csr
类似,但具有行方向。
当我在 MATLAB 中创建 FEM 刚度矩阵时,我使用了与 scipy
coo
输入等效的内容。即创建 data
、row
和 col
3 个数组。当 coo
转换为 csr
时,会添加重复元素,从而巧妙地处理 FEM 元素的子矩阵重叠。 (此行为在 scipy
和 MATLAB 中是相同的)。
像您一样重复添加 lil
矩阵应该可以工作(如果索引正确),但我预计它会慢得多。
关于python - 为什么在求解稀疏线性方程组时会出现内存错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56221189/
我在服务器上 checkout 了一个 git 存储库。该存储库过去在根目录下包含所有相关文件,但我必须进行一些更改,现在我有两个文件夹,src 和 dist,我想跟踪这两个文件夹. 我遇到的问题是,
我很难弄清楚 VkDescriptorSetLayoutBinding::binding 的任何用例,这是结构: struct VkDescriptorSetLayoutBinding { u
Python中能否有效获取稀疏向量的范数? 我尝试了以下方法: from scipy import sparse from numpy.linalg import norm vector1 = spa
我正在尝试找出为什么这段代码不对数组进行排序... 任意向量。 x = array([[3, 2, 4, 5, 7, 4, 3, 4, 3, 3, 1, 4, 6, 3, 2, 4, 3, 2]])
有谁知道如何压缩(编码)稀疏 vector ?稀疏 vector 表示有许多“0”的 1xN 矩阵。 例如 10000000000001110000000000000000100000000 上面是稀
我使用稀疏高斯过程进行 Rasmussen 回归。[http://www.tsc.uc3m.es/~miguel/downloads.php][1] 预测平均值的语法是: [~, mu_1, ~, ~
我在朴素贝叶斯分类器中使用 Mahout API。其中一个功能是 SparseVectorsFromSequenceFiles虽然我已经尝试过旧的谷歌搜索,但我仍然不明白什么是稀疏 vector 。最
我正在尝试将JavaScript稀疏数组映射到C#表示形式。 建议这样做的方法是什么? 它正在考虑使用一个字典,该字典包含在原始数组中包含值的原始词列表。 还有其他想法吗? 谢谢! 最佳答案 注意 针
如果我想求解一个完整上三角系统,我可以调用linsolve(A,b,'UT')。然而,这目前不支持稀疏矩阵。我该如何克服这个问题? 最佳答案 UT 和 LT 系统是最容易解决的系统之一。读一读on t
我有一个带有 MultiIndex 的 Pandas DataFrame。 MultiIndex 的值在 (0,0) 到 (1000,1000) 范围内,该列有两个字段 p 和 q. 但是,DataF
我目前正在实现一个小型有限元模拟。使用 Python/Numpy,我正在寻找一种有效的方法来创建全局刚度矩阵: 1)我认为应该使用coo_matrix()从较小的单元刚度矩阵创建稀疏矩阵。但是,我可以
a , b是 1D numpy ndarray与整数数据类型具有相同的大小。 C是一个 2D scipy.sparse.lil_matrix . 如果索引[a, b]包含重复索引,C[a, b] +=
我有一个大的、连通的、稀疏的邻接表形式的图。我想找到两个尽可能远的顶点,即 diameter of the graph以及实现它的两个顶点。 对于不同的应用程序,我对无向和有向情况下的这个问题都很感兴
上下文:我将 Eigen 用于人工神经网络,其中典型维度为每层约 1000 个节点。所以大部分操作是将大小为 ~(1000,1000) 的矩阵 M 与大小为 1000 的 vector 或一批 B v
我有一些大小合适的矩阵 (2000*2000),我希望在矩阵的元素中有符号表达式 - 即 .9**b + .8**b + .7**b ... 是一个元素的例子。矩阵非常稀疏。 我通过添加中间计算来创建
在 R 或 C++ 中是否有一种快速填充(稀疏)矩阵的方法: A, B, 0, 0, 0 C, A, B, 0, 0 0, C, A, B, 0 0, 0, C, A, B 0, 0, 0, C, A
我有一个大的稀疏 numpy/scipy 矩阵,其中每一行对应于高维空间中的一个点。我想进行以下类型的查询: 给定一个点P(矩阵中的一行)和一个距离epsilon,找到与epsilon距离最大的所有点
假设我有一个 scipy.sparse.csr_matrix 代表下面的值 [[0 0 1 2 0 3 0 4] [1 0 0 2 0 3 4 0]] 我想就地计算非零值的累积和,这会将数组更改为:
我了解如何在 Git 中配置稀疏 checkout ,但我想知道是否可以消除前导目录。例如,假设我有一个 Git 存储库,其文件夹结构如下: 文件夹1/foo 文件夹2/foo/bar/stuff 文
根据 this thread , Git 中的排除 sparse-checkout feature应该实现。是吗? 假设我有以下结构: papers/ papers/... presentations
我是一名优秀的程序员,十分优秀!