这是一个logistic sigmoid函数:

我知道x,现在如何用Python计算F(x) ?

设x = 0.458。

F(x) = ?


当前回答

我觉得很多人可能会对自由参数感兴趣来改变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

并绘制和比较:

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

最后:

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

其他回答

logistic s型函数的数值稳定版本。

    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)

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

我觉得很多人可能会对自由参数感兴趣来改变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

并绘制和比较:

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

最后:

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

另一种方法是变换tanh函数

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

可以计算为:

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

或概念性的,更深的,没有任何进口性的:

def sigmoid(x):
  return 1 / (1 + 2.718281828 ** -x)

或者你可以对矩阵使用numpy:

import numpy as np #make sure numpy is already installed
def sigmoid(x):
  return 1 / (1 + np.exp(-x))