我試圖計算兩個numpy數組分別(162225,10000)和(10000,100)大小的點積。但是,如果我調用numpy.dot(A,B)發生MemoryError。 我,然後,試着寫我的實現:numpy.dot - > MemoryError,my_dot - >非常慢,但可以工作。爲什麼?
def slower_dot (A, B):
"""Low-memory implementation of dot product"""
#Assuming A and B are of the right type and size
R = np.empty([A.shape[0], B.shape[1]])
for i in range(A.shape[0]):
for j in range(B.shape[1]):
R[i,j] = np.dot(A[i,:], B[:,j])
return R
,它工作得很好,但當然是很慢的。任何想法1)這種行爲背後的原因是什麼,2)我如何繞過/解決問題?
我在運行Ubuntu 14.10的16GB RAM的64位計算機上使用Python 3.4.2(64位)和Numpy 1.9.1。
爲了清楚這一點,你在運行一個64位版本的Python嗎? – filmor 2014-12-27 15:08:00
你可以試試'np.einsum'。我曾在np.dot返回內存錯誤或內存交換中斷的情況下使用它。 – hpaulj 2014-12-27 22:24:03
是的,它是一個64位版本。 – marcotama 2014-12-27 23:52:47