How to calculate a logistic sigmoid function in Python?

后端 未结 15 1885
情话喂你
情话喂你 2020-11-29 16:14

This is a logistic sigmoid function:

\"enter

I know x. How can I calculate F(x

15条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 17:07

    Here's how you would implement the logistic sigmoid in a numerically stable way (as described here):

    def sigmoid(x):
        "Numerically-stable sigmoid function."
        if x >= 0:
            z = exp(-x)
            return 1 / (1 + z)
        else:
            z = exp(x)
            return z / (1 + z)
    

    Or perhaps this is more accurate:

    import numpy as np
    
    def sigmoid(x):  
        return math.exp(-np.logaddexp(0, -x))
    

    Internally, it implements the same condition as above, but then uses log1p.

    In general, the multinomial logistic sigmoid is:

    def nat_to_exp(q):
        max_q = max(0.0, np.max(q))
        rebased_q = q - max_q
        return np.exp(rebased_q - np.logaddexp(-max_q, np.logaddexp.reduce(rebased_q)))
    

    (However, logaddexp.reduce could be more accurate.)

提交回复
热议问题