阅读79 返回首页    go 阿里云 go 技术社区[云栖]


神经网络常用激活函数对比:sigmoid VS sofmax(附python源码)

首发地址:https://yq.aliyun.com/articles/73661

https://yq.aliyun.com/cloud

Softmax

作者介绍:

f4fc34dbd7db4870aaad8adbe6801f02712e0bff

Saimadhu Polamuripython

领英:https://www.linkedin.com/in/saimadhu/

https://dataaspirant.com/author/saimadhu/

0a68f8e4007c67a30d310d506bd56c8951d7539b

Softmax
在学习逻辑回归概念时,主要的困惑于计算概率的函数由于在逻辑回归模型会使用计算的概率预测目标类经常用到的两个函数Softmaxd
函数水平(帮助预测目标类别)上来看,这两个函数是相同的,但存在许多明显的数学差异应用在深度学习和其他领域中,发挥至关重要的作用。
所以在这篇文章中将进一步了解这两个函数及其应用之间的根本区别。
在开始之前,介绍本文的目录

  • 什么是S型函数
  • S形函数性质
  • Sigmoid函数使用
  • Python
  • 创建Sigmoid函数图像 
  • 什么是Softmax函数
  • Softmax函数的性质
  • Softmax函数使用
  • Python
  • 创建Softmax函数图像 
  • Sigmoid
  • 结论 

S

数学定义上来看,Sigmoid实数,返回0曲线这些曲线也用于统计0
S

  • sigmoid
  • S

  1. 非负数:如果数字大于或等于零。
  2. 非正数:如果数字小于或等于零。

Sigmoid

  • Sigmoid
  • 创建人造神经元时,Sigmoid用作激活函数
  • S函数图像是常见的累积分布函数。
Python

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]

图像

成功运行上述代码后,屏幕上将显示以下图像。如果上述代码在你的系统中运行失败。检查机器学习包设置。 

2af24904fb9e1b80b569e854d65561ab3e0f697e

从上图可以看出,随着输入值的增加,sigmoid1表示在0.9
Softmax

ca81779093824ed50a586b53a6d102e83eb97d27

Softmaxn'在所有可能的目标类中的概率。计算的概率将有助于确定给定输入的目标类
Softmax范围0softmax,它会返回每个类的概率,并且目标类别的概率值会很大指数公式softmax 

Softmax

softmax函数的几个性质

  • 0
  • 1
Softmax使用

  • 用于多重分类逻辑回归模型。
  • 中,在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]

6softmax 

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]

 

c1385c479881cefd367307909716f29cbbae380a

softmax值越大,其概率越高
Sigmoid
Sigmoid差异表格:

56a1f13aeac2e59c5c725f9ec93c4f5bd7ea801d                        

结论
在本文中,详细了解确定逻辑回归模型的两个函数。

  • Softmax
  • Sigmoid

pdf

@

Difference Between Softmax Function and Sigmoid FunctionSaimadhu Polamuri

最后更新:2017-07-12 22:11:35

  上一篇:go  有监督相似性学习:基于相似问题数据的对称关系学习
  下一篇:go  论文导读:深度神经网络中的对抗样本与学习(附原文)