Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correct way of computing inner product with numba

I am trying to compute the inner product of two large matrices. it seems numpy create copy of matrices when trying to compute the dot product and it causes me some memory issues. After googling around I found numba package promising. However I can't make it work properly . Here is my code:

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)

with following running time:

dot1:
(0.034691810607910156, 'sec')

dot2:
(0.9215810298919678, 'sec')

can anyone tells me what I am missing here?

like image 836
Moj Avatar asked Nov 23 '25 06:11

Moj


1 Answers

Your algorithm is the naive algorithm. BLAS implements a faster one.

Quoting wikipedia's matrix multiplication page:

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

like image 129
simonzack Avatar answered Nov 25 '25 19:11

simonzack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!