Numpy中矩阵乘法的改进

2024-10-03 02:34:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我和一些朋友正在做一个小型的语言比赛来计算一些神经网络。有些是用C语言写的,有些是用fortran写的,还有我: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以38倍的优势落后于Fortran:-(

有没有改进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还是以同样的速度落后。你知道吗

好消息。Python在除了小型矩阵测试之外的所有测试中都取得了胜利。下面是整个代码:

Python函数(平淡的)你知道吗

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

测试执行功能(测试.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


Tags: testreturndefnprandomrealfunctionalrank
2条回答

如果你想让Numpy快一点,那就快一点。尝试卸载Numpy并安装英特尔优化版Numpy。英特尔优化版的Numpy包括许多CPU级别的优化,这些优化将显著提高使用英特尔CPU的计算机上的矩阵乘法等操作的性能。你知道吗

pip uninstall numpy
pip install intel-numpy

我们可以用matrix-multiplication来提高一点-

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

相关问题 更多 >