gpt4 book ai didi

python - 用numba计算内积的正确方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:03 24 4
gpt4 key购买 nike

我正在尝试计算两个大矩阵的内积。似乎 numpy 在尝试计算点积时创建了矩阵的副本,这导致了一些内存问题。谷歌搜索后,我发现 numba 包很有前途。但是我不能让它正常工作。这是我的代码:

import numpy as np
from numba import jit
import time, contextlib



@contextlib.contextmanager
def timeit():
t=time.time()
yield
print(time.time()-t,"sec")


def dot1(a,b):
return np.dot(a,b)

@jit(nopython=True)
def dot2(a,b):
n = a.shape[0]
m = b.shape[1]
K = b.shape[0]
c = np.zeros((n,m))
for i in xrange(n):
for j in xrange(m):
for k in range(K):
c[i,j] += a[i,k]*b[k,j]

return c



def main():
a = np.random.random((200,1000))
b = np.random.random((1000,400))

with timeit():
c1 = dot1(a,b)
with timeit():
c2 = dot2(a,b)

运行时间如下:

dot1:
(0.034691810607910156, 'sec')

dot2:
(0.9215810298919678, 'sec')

谁能告诉我我在这里缺少什么?

最佳答案

您的算法是朴素算法。 BLAS 实现了一个更快的。

引用维基百科的 matrix multiplication页:

Nevertheless, it appears in several libraries, such as BLAS, where it is significantly more efficient for matrices with dimensions n > 100

关于python - 用numba计算内积的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25545465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com