为什么使用物流sigmoid的tanh定义比scipy的expit更快?

Mat*_*ock 6 python numpy scipy

我正在为应用程序使用逻辑sigmoid.我比较了使用scipy.special函数的时间expit,而不是使用S形的双曲正切定义.

我发现双曲正切是快3倍.这里发生了什么?我还在排序的数组上测试了时间,看看结果是否有任何不同.

以下是在IPython中运行的示例:

In [1]: from scipy.special import expit

In [2]: myexpit = lambda x: 0.5*tanh(0.5*x) + 0.5

In [3]: x = randn(100000)

In [4]: allclose(expit(x), myexpit(x))
Out[4]: True

In [5]: timeit expit(x)
100 loops, best of 3: 15.2 ms per loop

In [6]: timeit myexpit(x)
100 loops, best of 3: 4.94 ms per loop

In [7]: y = sort(x)

In [8]: timeit expit(y)
100 loops, best of 3: 15.3 ms per loop

In [9]: timeit myexpit(y)
100 loops, best of 3: 4.37 ms per loop
Run Code Online (Sandbox Code Playgroud)

编辑:

机器信息:

  • Ubuntu 16.04
  • RAM:7.4 GB
  • 英特尔酷睿i7-3517U CPU @ 1.90GHz×4

Numpy/Scipy信息:

In [1]: np.__version__
Out[1]: '1.12.0'

In [2]: np.__config__.show()
lapack_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    define_macros = [('HAVE_CBLAS', None)]
    language = c
blas_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    define_macros = [('HAVE_CBLAS', None)]
    language = c
openblas_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    define_macros = [('HAVE_CBLAS', None)]
    language = c
blis_info:
  NOT AVAILABLE
openblas_lapack_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    define_macros = [('HAVE_CBLAS', None)]
    language = c
lapack_mkl_info:
  NOT AVAILABLE
blas_mkl_info:
  NOT AVAILABLE

In [3]: import scipy

In [4]: scipy.__version__
Out[4]: '0.18.1'
Run Code Online (Sandbox Code Playgroud)

Mat*_*ock 2

编辑:

我将向未来的人们推荐这个问题


总结有用评论的结果:

“为什么使用 tanh 定义逻辑 sigmoid 比 scipy 的 expit 更快?”

答:不是;tanh我的特定机器上的和C 函数发生了一些有趣的事情exp

事实证明,在我的机器上,C 函数 fortanhexp. 为什么会出现这种情况的答案显然属于另一个问题。当我运行下面列出的 C++ 代码时,我看到

tanh: 5.22203
exp: 14.9393
Run Code Online (Sandbox Code Playgroud)

从 Python 调用时,函数增加了约 3 倍tanh。奇怪的是,当我在具有相同操作系统的单独机器上运行相同的代码时,我得到了类似的计时结果tanhexp

#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;

int main() {
    double a = -5;
    double b =  5;
    int N =  10001;
    double x[10001];
    double y[10001];
    double h = (b-a) / (N-1);

    clock_t begin, end;

    for(int i=0; i < N; i++)
        x[i] = a + i*h;

    begin = clock();

    for(int i=0; i < N; i++)
        for(int j=0; j < N; j++)
            y[i] = tanh(x[i]);

    end = clock();

    cout << "tanh: " << double(end - begin) / CLOCKS_PER_SEC << "\n";

    begin = clock();

    for(int i=0; i < N; i++)
        for(int j=0; j < N; j++)
            y[i] = exp(x[i]);

    end = clock();

    cout << "exp: " << double(end - begin) / CLOCKS_PER_SEC << "\n";


    return 0;
}
Run Code Online (Sandbox Code Playgroud)