Numpy Pure Functions for performance, caching

后端 未结 4 1037
悲哀的现实
悲哀的现实 2020-11-28 14:06

I\'m writing some moderately performance critical code in numpy. This code will be in the inner most loop, of a computation that\'s run time is measured in hours. A quick ca

4条回答
  •  被撕碎了的回忆
    2020-11-28 14:33

    Mostly I agree with Warren Weckesser and his answer above. But for derivative of sigmoid the following can be used:

    In [002]: def sg(x):
         ...: s = scipy.special.expit(x)
         ...: return s * (1.0 - s) 
    

    Timings:

    In [003]: %timeit y = logistic._pdf(x)
    10000 loops, best of 3: 45 µs per loop
    
    In [004]: %timeit y = sg(x)
    10000 loops, best of 3: 20.4 µs per loop
    

    The only problem is accuracy:

    In [005]: sg(37)
    Out[005]: 0.0
    
    In [006]: logistic._pdf(37)
    Out[006]: 8.5330476257440658e-17    
    

提交回复
热议问题