如何在 Python 中计算逻辑 sigmoid 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3985619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:41:58  来源:igfitidea点击:

How to calculate a logistic sigmoid function in Python?

pythonsigmoid

提问by Richard Knop

This is a logistic sigmoid function:

这是一个逻辑 sigmoid 函数:

enter image description here

在此处输入图片说明

I know x. How can I calculate F(x) in Python now?

我知道 x。我现在如何在 Python 中计算 F(x)?

Let's say x = 0.458.

假设 x = 0.458。

F(x) = ?

F(x) = ?

采纳答案by unwind

This should do it:

这应该这样做:

import math

def sigmoid(x):
  return 1 / (1 + math.exp(-x))

And now you can test it by calling:

现在你可以通过调用来测试它:

>>> sigmoid(0.458)
0.61253961344091512

Update: Note that the above was mainly intended as a straight one-to-one translation of the given expression into Python code. It is nottested or known to be a numerically sound implementation. If you know you need a very robust implementation, I'm sure there are others where people have actually given this problem some thought.

更新:请注意,上述内容主要是将给定表达式直接一对一翻译成 Python 代码。它没有经过测试或已知是数字上可靠的实现。如果你知道你需要一个非常健壮的实现,我相信还有其他人实际上已经考虑过这个问题。

回答by ghostdog74

another way

其它的办法

>>> def sigmoid(x):
...     return 1 /(1+(math.e**-x))
...
>>> sigmoid(0.458)

回答by Théo T

It is also available in scipy: http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.logistic.html

它也可以在 scipy 中使用:http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.logistic.html

In [1]: from scipy.stats import logistic

In [2]: logistic.cdf(0.458)
Out[2]: 0.61253961344091512

which is only a costly wrapper (because it allows you to scale and translate the logistic function) of another scipy function:

这只是另一个 scipy 函数的昂贵包装器(因为它允许您缩放和转换逻辑函数):

In [3]: from scipy.special import expit

In [4]: expit(0.458)
Out[4]: 0.61253961344091512

If you are concerned about performances continue reading, otherwise just use expit.

如果您担心性能,请继续阅读,否则只需使用expit.

Some benchmarking:

一些基准测试:

In [5]: def sigmoid(x):
  ....:     return 1 / (1 + math.exp(-x))
  ....: 

In [6]: %timeit -r 1 sigmoid(0.458)
1000000 loops, best of 1: 371 ns per loop


In [7]: %timeit -r 1 logistic.cdf(0.458)
10000 loops, best of 1: 72.2 μs per loop

In [8]: %timeit -r 1 expit(0.458)
100000 loops, best of 1: 2.98 μs per loop

As expected logistic.cdfis (much) slower than expit. expitis still slower than the python sigmoidfunction when called with a single value because it is a universal function written in C ( http://docs.scipy.org/doc/numpy/reference/ufuncs.html) and thus has a call overhead. This overhead is bigger than the computation speedup of expitgiven by its compiled nature when called with a single value. But it becomes negligible when it comes to big arrays:

正如预期的那样logistic.cdf(远)慢于expit. 使用单个值调用时expit仍然比 pythonsigmoid函数慢,因为它是用 C ( http://docs.scipy.org/doc/numpy/reference/ufuncs.html)编写的通用函数,因此有调用开销。expit当使用单个值调用时,此开销大于其编译性质给出的计算加速。但是当涉及到大数组时,它变得可以忽略不计:

In [9]: import numpy as np

In [10]: x = np.random.random(1000000)

In [11]: def sigmoid_array(x):                                        
   ....:    return 1 / (1 + np.exp(-x))
   ....: 

(You'll notice the tiny change from math.expto np.exp(the first one does not support arrays, but is much faster if you have only one value to compute))

(您会注意到从math.expto的微小变化np.exp(第一个不支持数组,但如果您只有一个值要计算,速度会快得多))

In [12]: %timeit -r 1 -n 100 sigmoid_array(x)
100 loops, best of 1: 34.3 ms per loop

In [13]: %timeit -r 1 -n 100 expit(x)
100 loops, best of 1: 31 ms per loop

But when you really need performance, a common practice is to have a precomputed table of the the sigmoid function that hold in RAM, and trade some precision and memory for some speed (for example: http://radimrehurek.com/2013/09/word2vec-in-python-part-two-optimizing/)

但是当你真的需要性能时,一个常见的做法是在 RAM 中预先计算一个 sigmoid 函数的表,并用一些精度和内存来换取一些速度(例如:http: //radimrehurek.com/2013/09 /word2vec-in-python-part-two-optimizing/)

Also, note that expitimplementation is numerically stable since version 0.14.0: https://github.com/scipy/scipy/issues/3385

另外,请注意,expit自 0.14.0 版以来,实现在数值上是稳定的:https: //github.com/scipy/scipy/issues/3385

回答by Neil G

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.

在内部,它实现与上述相同的条件,但随后使用log1p.

In general, the multinomial logistic sigmoid is:

一般来说,多项logistic sigmoid是:

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.reducecould be more accurate.)

(但是,logaddexp.reduce可能更准确。)

回答by czxttkl

Good answer from @unwind. It however can't handle extreme negative number (throwing OverflowError).

来自@unwind 的好答案。但是它不能处理极端负数(抛出溢出错误)。

My improvement:

我的改进:

def sigmoid(x):
    try:
        res = 1 / (1 + math.exp(-x))
    except OverflowError:
        res = 0.0
    return res

回答by dontloo

Another way by transforming the tanhfunction:

转换tanh函数的另一种方法:

sigmoid = lambda x: .5 * (math.tanh(.5 * x) + 1)

回答by Philipp Schwarz

I feel many might be interested in free parameters to alter the shape of the sigmoid function. Second for many applications you want to use a mirrored sigmoid function. Third you might want to do a simple normalization for example the output values are between 0 and 1.

我觉得很多人可能对改变 sigmoid 函数形状的自由参数感兴趣。其次,对于您想要使用镜像 sigmoid 函数的许多应用程序。第三,您可能想要进行简单的归一化,例如输出值介于 0 和 1 之间。

Try:

尝试:

def normalized_sigmoid_fkt(a, b, x):
   '''
   Returns array of a horizontal mirrored normalized sigmoid function
   output between 0 and 1
   Function parameters a = center; b = width
   '''
   s= 1/(1+np.exp(b*(x-a)))
   return 1*(s-min(s))/(max(s)-min(s)) # normalize function to 0-1

And to draw and compare:

并绘制和比较:

def draw_function_on_2x2_grid(x): 
    fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
    plt.subplots_adjust(wspace=.5)
    plt.subplots_adjust(hspace=.5)

    ax1.plot(x, normalized_sigmoid_fkt( .5, 18, x))
    ax1.set_title('1')

    ax2.plot(x, normalized_sigmoid_fkt(0.518, 10.549, x))
    ax2.set_title('2')

    ax3.plot(x, normalized_sigmoid_fkt( .7, 11, x))
    ax3.set_title('3')

    ax4.plot(x, normalized_sigmoid_fkt( .2, 14, x))
    ax4.set_title('4')
    plt.suptitle('Different normalized (sigmoid) function',size=10 )

    return fig

Finally:

最后:

x = np.linspace(0,1,100)
Travel_function = draw_function_on_2x2_grid(x)

Sigmoid functions graph

Sigmoid 函数图

回答by Enrique Pérez Herrero

Tensorflow includes also a sigmoidfunction: https://www.tensorflow.org/versions/r1.2/api_docs/python/tf/sigmoid

Tensorflow 还包括一个sigmoid函数:https: //www.tensorflow.org/versions/r1.2/api_docs/python/tf/sigmoid

import tensorflow as tf

sess = tf.InteractiveSession()
x = 0.458
y = tf.sigmoid(x)

u = y.eval()
print(u)
# 0.6125396

回答by Yash Khare

A numerically stable version of the logistic sigmoid function.

逻辑 sigmoid 函数的数值稳定版本。

    def sigmoid(x):
        pos_mask = (x >= 0)
        neg_mask = (x < 0)
        z = np.zeros_like(x,dtype=float)
        z[pos_mask] = np.exp(-x[pos_mask])
        z[neg_mask] = np.exp(x[neg_mask])
        top = np.ones_like(x,dtype=float)
        top[neg_mask] = z[neg_mask]
        return top / (1 + z)

回答by Diatche

Use the numpy package to allow your sigmoid function to parse vectors.

使用 numpy 包允许您的 sigmoid 函数解析向量。

In conformity with Deeplearning, I use the following code:

根据 Deeplearning,我使用以下代码:

import numpy as np
def sigmoid(x):
    s = 1/(1+np.exp(-x))
    return s