对稀疏矩阵执行外积之和
我正在尝试使用 scipy 的稀疏包来实现以下方程
W = x[:,1] * y[:,1].T + x[:,2] * y[:,2].T + ...
: y 是一个 nxm csc_matrix。基本上,我试图将 x 的每一列乘以 y 的每一列,并将所得的 nxn 矩阵相加。然后我想让所有非零元素为 1。
这是我当前的实现:
c = sparse.csc_matrix((n, n))
for i in xrange(0,m):
tmp = bam.id2sym_thal[:,i] * bam.id2sym_cort[:,i].T
minimum(tmp.data,ones_like(tmp.data),tmp.data)
maximum(tmp.data,ones_like(tmp.data),tmp.data)
c = c + tmp
此实现存在以下问题:
内存使用量似乎激增。据我了解,内存只会随着 c 变得不那么稀疏而增加,但我看到循环开始消耗 >20GB 的内存,an=10,000,m=100,000(x 和 y 的每一行只有大约 60非零元素)。
我使用的 python 循环效率不是很高。
我的问题:有更好的方法吗?控制内存使用是我首先关心的问题,但如果能够让它更快的话那就太好了!
谢谢你!
I am trying to implement the following equation using scipy's sparse package:
W = x[:,1] * y[:,1].T + x[:,2] * y[:,2].T + ...
where x & y are a nxm csc_matrix. Basically I'm trying to multiply each col of x by each col of y and sum the resulting nxn matrices together. I then want to make all non-zero elements 1.
This is my current implementation:
c = sparse.csc_matrix((n, n))
for i in xrange(0,m):
tmp = bam.id2sym_thal[:,i] * bam.id2sym_cort[:,i].T
minimum(tmp.data,ones_like(tmp.data),tmp.data)
maximum(tmp.data,ones_like(tmp.data),tmp.data)
c = c + tmp
This implementation has the following problems:
Memory usage seems to explode. As I understand it, memory should only increase as c becomes less sparse, but I am seeing that the loop starts eating up >20GB of memory with a n=10,000, m=100,000 (each row of x & y only has around 60 non-zero elements).
I'm using a python loop which is not very efficient.
My question: Is there a better way to do this? Controlling memory usage is my first concern, but it would be great to make it faster!
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,以您描述的方式求出的外积之和与将两个矩阵相乘相同。换句话说,
只需将矩阵相乘即可。
对于 n=10000 和 m=100000 并且每列在 X 和 Y 中都有一个非零元素,它几乎可以立即在我的笔记本电脑上进行计算。
Note that a sum of outer products in the manner you describe is simply the same as multiplying two matrices together. In other words,
So just multiply the matrices together.
For n=10000 and m=100000 and where each column has one nonzero element in both X and Y, it computes almost instantly on my laptop.
就内存和性能而言,这可能是使用 Cython 的主要候选者。
以下论文中有一部分描述了它在稀疏 scipy 矩阵中的使用:
http://folk.uio .no/dagss/cython_cise.pdf
In terms of memory and performance, this might be a prime candidate for using Cython.
There is a section of the following paper describing its use with sparse scipy matricies:
http://folk.uio.no/dagss/cython_cise.pdf