expip函数,在scipy.special中,是一个矢量化的sigmoid函数。它计算1 /(1 + e ^( - x)),这很复杂,可能涉及泰勒级数。
我了解了“快速sigmoid”,1 /(1 + abs(x)),它应该快得多 - 但内置的expit函数大大优于它,即使我将它作为lambda表达式传递给它numpy.vectorize。
这是测试它们的一种方法:
from scipy.special import expit
data = np.random.rand(1000000)
内置的,复杂的sigmoid很快:
%prun expit(data)
3 function calls in 0.064 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.064 0.064 0.064 0.064 <string>:1(<module>)
1 0.000 0.000 0.064 0.064 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
更简单的sigmoid大约慢20倍:
%prun np.vectorize( lambda x: (x / (1 + abs(x)) + 1) / 2 )(data)
2000023 function calls in 1.992 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1000001 1.123 0.000 1.294 0.000 <string>:1(<lambda>)
1 0.558 0.558 1.950 1.950 function_base.py:2276(_vectorize_call)
1000001 0.170 0.000 0.170 0.000 {built-in method builtins.abs}
4 0.098 0.025 0.098 0.025 {built-in method numpy.core.multiarray.array}
1 0.041 0.041 1.991 1.991 function_base.py:2190(__call__)
1 0.000 0.000 0.068 0.068 function_base.py:2284(<listcomp>)
1 0.000 0.000 1.992 1.992 {built-in method builtins.exec}
1 0.000 0.000 1.991 1.991 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 function_base.py:2220(_get_ufunc_and_otypes)
1 0.000 0.000 0.000 0.000 function_base.py:2162(__init__)
1 0.000 0.000 0.000 0.000 function_base.py:2242(<listcomp>)
2 0.000 0.000 0.000 0.000 numeric.py:414(asarray)
1 0.000 0.000 0.000 0.000 {built-in method numpy.core.umath.frompyfunc}
1 0.000 0.000 0.000 0.000 function_base.py:2266(<listcomp>)
2 0.000 0.000 0.000 0.000 {built-in method builtins.isinstance}
1 0.000 0.000 0.000 0.000 {built-in method builtins.len}
1 0.000 0.000 0.000 0.000 {method 'join' of 'str' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}