激活函数 Sigmod 及其导数

1. Sigmod 函数

Sigmod 函数是神经网络中最常用的激活函数之一,其形式如下:
sigmod ( x ) = f ( x ) = 1 1 + e − x . \text{sigmod}(x) = f(x) = \frac{1}{1 + e^{-x}}. sigmod(x)=f(x)=1+ex1.

2. 取值

分析:
(1)当 x = 0 x=0 x=0 时,
f ( x ) = f ( 0 ) = 1 1 + e − 0 = 1 2 . f(x) = f(0) = \frac{1}{1+e^{-0}} = \frac{1}{2}. f(x)=f(0)=1+e01=21.
(2)当 x → + ∞ x \to +\infty x+ 时, e − x → 0 e^{-x} \to 0 ex0,由此 f ( x ) → 1 f(x) \to 1 f(x)1
(3)当 x → − ∞ x \to -\infty x 时, e − x → + ∞ e^{-x} \to +\infty ex+,由此 f ( x ) → 0 f(x) \to 0 f(x)0

由此,sigmod 函数的取值范围是 [ 0 , 1 ] [0, 1] [0,1],且单调递增。

3. 图像

用 Python 来展示它的部分图像:
在这里插入图片描述

实现的 Python 代码如下:

import numpy as np
import matplotlib.pyplot as plt


def sigmoid(x: np.ndarray) -> np.ndarray:
    """计算Sigmoid函数值"""
    return 1 / (1 + np.exp(-x))


def plot_sigmoid_origin() -> None:
    """
    绘制原点在(0,0)的Sigmoid函数图像
    """
    # 生成数据
    x = np.linspace(-10, 10, 400)
    y = sigmoid(x)

    # 创建画布和坐标轴
    fig, ax = plt.subplots(figsize=(10, 6), dpi=100)

    # 绘制曲线
    ax.plot(x, y,
            label=r'$\sigma(x) = \frac{1}{1 + e^{-x}}$',
            color='#E63946',  # 更现代的红色
            linewidth=2.5,
            zorder=3)

    # 设置坐标轴范围
    ax.set_xlim(-10, 10)
    ax.set_ylim(-0.1, 1.1)  # 保持上方留白

    # 移动坐标轴到原点
    ax.spines['left'].set_position('zero')
    ax.spines['bottom'].set_position('zero')
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

    # 设置刻度参数
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    ax.tick_params(axis='both', which='major', labelsize=10)

    # 自定义刻度标签
    ax.set_xticks(np.arange(-10, 11, 2))
    ax.set_yticks([0, 0.5, 1.0])
    ax.set_yticklabels(['0', r'$\frac{1}{2}$', '1'])  # 分数显示

    # 添加辅助网格
    ax.grid(True,
            linestyle=':',
            color='gray',
            alpha=0.6,
            zorder=0)

    # 标注关键点
    ax.scatter(0, 0.5, color='#2A9D8F', s=80, zorder=4)
    ax.annotate(r'$(0, \frac{1}{2})$',
                xy=(0, 0.5),
                xytext=(2, 0.6),
                arrowprops=dict(arrowstyle='->', color='black'),
                fontsize=12)

    # 添加渐近线
    ax.axhline(1, color='#264653', linestyle='--', alpha=0.5, zorder=1)
    ax.axhline(0, color='#264653', linestyle='--', alpha=0.5, zorder=1)

    # 设置标签和标题
    ax.set_xlabel('x', fontsize=12, loc='right')
    ax.set_ylabel(r'$\sigma(x)$', fontsize=12, loc='top', rotation=0)
    ax.set_title('Sigmoid 函数',
                 pad=20,
                 fontsize=14,
                 color='#2A9D8F')

    # 添加图例
    ax.legend(loc='upper left', framealpha=0.95)

    plt.tight_layout()
    plt.show()


# 执行绘图
if __name__ == "__main__":
    plot_sigmoid_origin()

4. 导数

f ′ ( x ) = ( 1 1 + e − x ) ′ = − 1 ( 1 + e − x ) 2 ( 1 + e − x ) ′ = − 1 ( 1 + e − x ) 2 e − x ( − x ) ′ = − 1 ( 1 + e − x ) 2 e − x ( − 1 ) = e − x ( 1 + e − x ) 2 = ( 1 − 1 ) + e − x ( 1 + e − x ) 2 = ( 1 + e − x ) − 1 ( 1 + e − x ) 2 = 1 1 + e − x − 1 ( 1 + e − x ) 2 = f ( x ) − f 2 ( x ) = f ( x ) ( 1 − f ( x ) ) . \begin{aligned} f'(x) &= \left(\frac{1}{1+e^{-x}} \right)' \\ &= - \frac{1}{\left(1+e^{-x}\right)^2} \left(1+e^{-x} \right)' \\ &= - \frac{1}{\left(1+e^{-x}\right)^2} e^{-x} (-x)' \\ &= - \frac{1}{\left(1+e^{-x}\right)^2} e^{-x} (-1) \\ &= \frac{e^{-x}}{\left(1+e^{-x}\right)^2} = \frac{(1-1) + e^{-x}}{\left(1+e^{-x}\right)^2} = \frac{(1 + e^{-x}) -1}{\left(1+e^{-x}\right)^2} \\ &= \frac{1}{1 + e^{-x}} - \frac{1}{\left(1 + e^{-x}\right)^2} \\ &= f(x) - f^2(x) \\ &= f(x) \left(1-f(x) \right). \end{aligned} f(x)=(1+ex1)=(1+ex)21(1+ex)=(1+ex)21ex(x)=(1+ex)21ex(1)=(1+ex)2ex=(1+ex)2(11)+ex=(1+ex)2(1+ex)1=1+ex1(1+ex)21=f(x)f2(x)=f(x)(1f(x)).

即: f ′ ( x ) = f ( x ) ( 1 − f ( x ) ) f'(x) = f(x) \left(1-f(x) \right) f(x)=f(x)(1f(x))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值