改善Numpy中的矩阵乘法

时间:2018-11-05 22:29:36

标签: python numpy fortran matrix-multiplication

我和一些朋友正在做一场小型语言竞赛,以计算一些神经网络。我在fortran中用C做的其他事情,还有我:Python。

代码很简单,只是一堆矢量点运算,然后是求和,然后应用信号函数并返回-1或1(是否激活)。

因此,我们将发送一堆随机数,并检查(现在只有单个进程)哪种语言能更快地完成该任务。

我的代码很简单:

def sgn(h):
    """Signal function"""
    return -1 if h < 0 else 1

def lincomb(A, B):
    """Linear combinator between two matrices"""
    return np.einsum('ji,ij->', A, B)

def lincombrav(A, B):
return A.ravel().dot(B.ravel('F'))

def functional_test():
    w1 = np.random.random(50**2).reshape(50,50)
    w2 = np.random.random(50**2).reshape(50,50)
    return sgn(lincombrav(w1, w2))

其中A和B是代表神经网络中每一层的矩阵。然后我们将第一个矩阵的第i个列与第二个矩阵的第i个行相加,将所有结果求和并发送给信号函数。像这样:

w1 = 2*np.random.random(100**2).reshape(100,100)-1
w2 = 2*np.random.random(100**2).reshape(100,100)-1

然后我们用它计时

%timeit sgn(lincomb(w1, w2))

Python输给了Fortran 38倍:-(

无论如何,有没有改进Python的“代码”。

编辑:添加了时间结果:

Python版本(已使用ravel模式)

In [10]: %timeit functional_test()
8.72 µs ± 406 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Python版本(带有einsum

In [16]: %timeit functional_test()
10.27 µs ± 490 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Fortran版本

In [13]: %timeit fort.test()
235 ns ± 12.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Fortran版本是使用“ f2py”程序创建的,用于从fortran代码中生成python可加载模块。

测试功能执行以下操作(每种语言):

  1. 创建矩阵A
  2. 创建矩阵B
  3. 从每种相应的语言实现中调用sgn(lincomb(A,B))#

我还将矩阵创建移到外部,以便仅运行数学运算,而不是处理内存。不过,python落后了同样的幅度。

EDIT2:好消息。 Python赢得了除小型矩阵测试之外的所有测试。这里将遵循完整的代码:

Python函数(bla.py)

import numpy as np
from numba import jit
import timeit
import matplotlib.pyplot as plt

def sgn(h):
    """Signal function"""
    return -1 if h < 0 else 1

def lincomb(A, B):
    """Linear combinator between two matrices"""
    return np.einsum('ji,ij->', A, B)

def lincombrav(A, B):
    return A.ravel().dot(B.ravel('F'))

def functional_test_ravel(n):
    """Functional tests (Victor experiment)"""

    w = 2*np.random.random(n**2).reshape(n,n)-1
    x = 2*np.random.random(n**2).reshape(n,n)-1

    return sgn(lincombrav(w, x))

def functional_test_einsum(n):
    """Functional tests (Victor experiment)"""

    w = 2*np.random.random(n**2).reshape(n,n)-1
    x = 2*np.random.random(n**2).reshape(n,n)-1

    return  sgn(lincomb(w, x))

@jit()
def functional_test_numbaein(n):
    """Functional tests (Victor experiment)"""

    w = 2*np.random.random(n**2).reshape(n,n)-1
    x = 2*np.random.random(n**2).reshape(n,n)-1

    return sgn(lincomb(w, x))


@jit()
def functional_test_numbarav(n):
    """Functional tests (Victor experiment)"""

    w = 2*np.random.random(n**2).reshape(n,n)-1
    x = 2*np.random.random(n**2).reshape(n,n)-1

    return sgn(lincombrav(w, x))

Fortran函数(fbla.f95)

module fbla
    implicit none
    integer, parameter::dp = selected_real_kind(12,100)
    public

contains

    real(kind=dp) function sgn(x)
        integer, parameter::dp = selected_real_kind(12,100)
        real(kind=dp), intent(in):: x

        if(x >= 0.0 ) then
            sgn = +1.0   
        else if (x < 0.0) then
            sgn = -1.0 
        end if
    end function sgn

    real(kind=dp) function lincomb(A, B, n)
        integer, parameter :: sp = selected_int_kind(r=8)
        integer, parameter :: dp = selected_real_kind(12,100)

        integer(kind=sp) :: i
        integer(kind=sp), intent(in):: n
        real(kind=DP), intent(in) :: A(n,n)
        real(kind=DP), intent(in) :: B(n,n)

        lincomb = 0
        do i=1,n
            lincomb = lincomb + dot_product(A(:,i),B(i,:))
        end do
    end function lincomb

    real(kind=dp) function functional_test(n)
        integer, parameter::dp = selected_real_kind(12,100)
        integer, parameter::sp = selected_int_kind(r=8)

        integer(kind=sp), intent(in):: n
        integer(kind=sp):: i, j
        real(kind=dp), allocatable, dimension(:,:):: x, w, wt   

        ALLOCATE(wt(n,n),w(n,n),x(n,n))

        do i=1,n
            do j=1,n
                w(i,j) = 2*rand(0)-1
                x(i,j) = 2*rand(0)-1
            end do
        end do

        wt = transpose(w)
        functional_test = sgn(lincomb(wt, x, n))
    end function functional_test

end module fbla

测试执行功能(tests.py)

import numpy as np
import timeit
import matplotlib.pyplot as plt
import bla
from fbla import fbla

def run_test(test_functions, N, runs=1000):
    results = []
    global rank
    for n in N:
        rank = n
        for t in test_functions:
            # print(f'Rank {globals()["rank"]}')
            print(f'Running {t} to matrix size {rank}', end='')
            r = min(timeit.Timer(t , globals=globals()).repeat(repeat=5, number=runs))
            print(f' total time {r} per run {r/runs}')
            results.append((t, n, r, r/runs))

    return results


def plotbars(results, test_functions, N):
    Nsz = len(N)
    M = len(test_functions)

    fig, ax = plt.subplots()

    ind = np.arange(int(Nsz))
    width = 1/(M+1)

    p = []
    for n in range(M):
        g = [ w*1000 for (x,y,z,w) in results if x==test_functions[n]]
        p.append(ax.bar(ind+n*width, g, width, bottom=0))

    ax.legend([ l[0] for l in p ], test_functions)
    ax.set_xticks(ind-width/2+((M/2) * width))
    ax.set_xticklabels(np.array(N).astype(str))
    ax.set_xlabel('Rank of square random matrix')
    ax.set_ylabel('Average time(ms) per run')
    ax.set_yscale('log')

    return fig

N = (10, 50, 100, 1000)
test_functions = [ 
        'bla.functional_test_einsum(rank)', 
        'fbla.functional_test(rank)'
]

results = run_test(test_functions, N)
plot = plotbars(results, test_functions, N)
plot.show()

结果是:

[('bla.functional_test_einsum(rank)', 10, 0.023221354000270367, 2.3221354000270368e-05),
 ('fbla.functional_test(rank)', 10, 0.005375514010665938, 5.375514010665938e-06),
 ('bla.functional_test_einsum(rank)', 50, 0.07035048000398092, 7.035048000398091e-05),
 ('fbla.functional_test(rank)', 50, 0.1242617039824836, 0.0001242617039824836),
 ('bla.functional_test_einsum(rank)', 100, 0.22694124400732107, 0.00022694124400732108),
 ('fbla.functional_test(rank)', 100, 0.5518505079962779, 0.0005518505079962779),
 ('bla.functional_test_einsum(rank)', 1000, 37.88827919398318, 0.03788827919398318),
 ('fbla.functional_test(rank)', 1000, 74.09929457501858, 0.07409929457501857)]

timeit会话中的某些标准ipython3输出。 fbla是fortran库,而bla是标准python库。

In : n=1000
In : w1 = 2*np.random.random(n**2).reshape(n,n)-1
In : w2 = 2*np.random.random(n**2).reshape(n,n)-1

In : bla.sgn(bla.lincomb(w1,w2))
Out: -1

In : fbla.sgn(fbla.lincomb(w1,w2))
Out: -1.0

In : %timeit fbla.sgn(fbla.lincomb(w1,w2))
11.3 ms ± 430 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In : %timeit bla.sgn(bla.lincomb(w1,w2))
3.81 ms ± 573 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Python wins

2 个答案:

答案 0 :(得分:0)

我们可以使用matrix-multiplication-

sgn(w1.ravel().dot(w2.ravel('F')))

答案 1 :(得分:0)

如果您希望Numpy更快,请获得更快的Numpy。尝试卸载Numpy并安装Intel优化版本的Numpy。英特尔的Numpy优化版本包括许多CPU级别的优化,可以显着提高运算性能,例如使用英特尔CPU的计算机上的矩阵乘法。

pip uninstall numpy
pip install intel-numpy