神经网络常用激活函数对比:sigmoid VS sofmax(附python源码)
首发地址:https://yq.aliyun.com/articles/73661
Softmax
作者介绍:
Saimadhu Polamuripython
领英:https://www.linkedin.com/in/saimadhu/
https://dataaspirant.com/author/saimadhu/
Softmax
在学习逻辑回归概念时,主要的困惑在于计算概率的函数,由于在逻辑回归模型中会使用计算出的概率来预测目标类别,经常用到的两个函数Softmaxd。
从函数水平(帮助预测目标类别)上来看,这两个函数是相同的,但存在许多明显的数学差异,应用在深度学习和其他领域中,发挥了至关重要的作用。
所以在这篇文章中将进一步了解这两个函数及其应用之间的根本区别。
在开始之前,介绍本文的目录:
- 什么是S型函数?
- S形函数的性质
- Sigmoid函数的使用
- Python
- 创建Sigmoid函数图像形
- 什么是Softmax函数?
- Softmax函数的性质
- Softmax函数的使用
- Python
- 创建Softmax函数图像形
- Sigmoid
-
结论
S?
从数学定义上来看,Sigmoid的实数,返回的0型曲线,这些曲线也用于统计,0。
S
- sigmoid
- S
- 非负数:如果数字大于或等于零。
- 非正数:如果数字小于或等于零。
Sigmoid
- Sigmoid
-
创建人造神经元时,Sigmoid用作激活函数。
-
S函数图像是常见的累积分布函数。
Python
# Required Python Package
import numpy as np
def sigmoid(inputs):
"""
Calculate the sigmoid for the give inputs (array)
:param inputs:
:return:
"""
sigmoid_scores = [1 / float(1 + np.exp(- x)) for x in inputs]
return sigmoid_scores
sigmoid_inputs = [2, 3, 5, 6]
print "Sigmoid Function Output :: {}".format(sigmoid(sigmoid_inputs))
Sigmoid代码。
- 该函数将以列表形式的值作为输入参数。
- /
- 代码 1 / float(1 + np.exp(-x))是用于计算sigmoid分数的函数。
-
接下来,我们将一个列表sigmiod_inputs作为函数的输入,列表值为2,3,5,6,经过sigmoid函数计算后获得Sigmoid分数。
脚本输出
Sigmoid Function Output :: [0.8807970779778823, 0.9525741268224334, 0.9933071490757153, 0.9975273768433653]
Sigmoid
现在使用上面的函数来创建图像,以方便Sigmoid
-
0
-
输入sigmoid
- 图像。
# Required Python Packages
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(inputs):
"""
Calculate the sigmoid for the give inputs (array)
:param inputs:
:return:
"""
sigmoid_scores = [1 / float(1 + np.exp(- x)) for x in inputs]
return sigmoid_scores
def line_graph(x, y, x_title, y_title):
"""
Draw line graph with x and y values
:param x:
:param y:
:param x_title:
:param y_title:
:return:
"""
plt.plot(x, y)
plt.xlabel(x_title)
plt.ylabel(y_title)
plt.show()
graph_x = range(0, 21)
graph_y = sigmoid(graph_x)
print "Graph X readings: {}".format(graph_x)
print "Graph Y readings: {}".format(graph_y)
line_graph(graph_x, graph_y, "Inputs", "Sigmoid Scores")
-
0
-
graph_ysigmoid
-
line_graph图像x
脚本输出
Graph X readings: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Graph Y readings: [0.5, 0.7310585786300049, 0.8807970779778823, 0.9525741268224334, 0.9820137900379085, 0.9933071490757153, 0.9975273768433653, 0.9990889488055994, 0.9996646498695336, 0.9998766054240137, 0.9999546021312976, 0.999983298578152, 0.9999938558253978, 0.999997739675702, 0.9999991684719722, 0.999999694097773, 0.9999998874648379, 0.9999999586006244, 0.9999999847700205, 0.9999999943972036, 0.9999999979388463]
图像
成功运行上述代码后,屏幕上将显示以下图像。如果上述代码在你的系统中运行失败。检查机器学习包的设置。
从上图可以看出,随着输入值的增加,sigmoid1表示在0.9的值。
Softmax?
Softmaxn'别在所有可能的目标类中的概率。计算出的概率将有助于确定给定输入的目标类别。
Softmax的范围,0将softmax,它会返回每个类别的概率,并且目标类别的概率值会很大。指数公式softmax
Softmax
softmax函数的几个性质。
-
0
-
1
- 用于多重分类逻辑回归模型。
- 中,在softmax
Python
Python
# Required Python Package
import numpy as np
def softmax(inputs):
"""
Calculate the softmax for the give inputs (array)
:param inputs:
:return:
"""
return np.exp(inputs) / float(sum(np.exp(inputs)))
softmax_inputs = [2, 3, 5, 6]
print "Softmax Function Output :: {}".format(softmax(softmax_inputs))
脚本输出
Softmax Function Output :: [ 0.01275478 0.03467109 0.25618664 0.69638749]
Softmax
Softmax图像来了解这个函数的表现。
-
0
-
将通过此列表来计算已实现函数的分数。
- 创建图像。
# Required Python Packages
import numpy as np
import matplotlib.pyplot as plt
def softmax(inputs):
"""
Calculate the softmax for the give inputs (array)
:param inputs:
:return:
"""
return np.exp(inputs) / float(sum(np.exp(inputs)))
def line_graph(x, y, x_title, y_title):
"""
Draw line graph with x and y values
:param x:
:param y:
:param x_title:
:param y_title:
:return:
"""
plt.plot(x, y)
plt.xlabel(x_title)
plt.ylabel(y_title)
plt.show()
graph_x = range(0, 21)
graph_y = softmax(graph_x)
print "Graph X readings: {}".format(graph_x)
print "Graph Y readings: {}".format(graph_y)
line_graph(graph_x, graph_y, "Inputs", "Softmax Scores")
脚本输出
Graph X readings: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Graph Y readings: [ 1.30289758e-09 3.54164282e-09 9.62718331e-09 2.61693975e-08 7.11357976e-08 1.93367146e-07 5.25626399e-07 1.42880069e-06 3.88388295e-06 1.05574884e-05 2.86982290e-05 7.80098744e-05 2.12052824e-04 5.76419338e-04 1.56687021e-03 4.25919483e-03 1.15776919e-02 3.14714295e-02 8.55482149e-02 2.32544158e-01 6.32120559e-01]
softmax,值越大,其概率越高。
Sigmoid
Sigmoid差异表格:
结论
在本文中,详细了解确定逻辑回归模型的两个函数。
- Softmax
-
Sigmoid
Difference Between Softmax Function and Sigmoid FunctionSaimadhu Polamuri
最后更新:2017-07-12 22:11:35