我正在为应用程序使用逻辑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
机器信息:
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'
答案 0 :(得分:2)
我将未来的人称为this question。
总结有用评论的结果:
"为什么使用物流sigmoid的tanh定义比scipy的expit更快?"
答案:不是;在我的特定机器上使用tanh
和exp
C函数进行了一些有趣的业务。
事实证明,在我的机器上,tanh
的C函数比exp
快。为什么会出现这种情况的答案显然属于另一个问题。当我运行下面列出的C ++代码时,我看到了
tanh: 5.22203
exp: 14.9393
,当从Python调用时匹配tanh
函数的〜3x增加。奇怪的是,当我在具有相同操作系统的单独机器上运行相同的代码时,我得到tanh
和exp
的类似时序结果。
#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;
}
答案 1 :(得分:0)
实际上,我认为您的分析是不正确的。我在机器上观察到类似的问题,我认为这是由scipy expit定义中的if
引起的。在C ++上,在I exp之上运行脚本比在tanh中运行更快。但是运行python代码段后,通过使用tanh而不是expit可以使速度提高3倍。