• 深度学习笔记(51) 基础知识


    深度学习 基础知识


    1. sigmoid

    a = 1 1 + e − Z a = \frac{1}{1 + e^{-Z}} a=1+eZ1 ,值域介于0和1之间
    sigmoid

    import numpy as np
    import matplotlib.pyplot as plt
    
    Z = np.linspace(-10, 10, 100)
    a = 1 / (1 + np.exp(-Z))
    
    fig = plt.figure()
    plt.plot(Z, a, color="blue", linewidth=1, linestyle="-", label="sigmoid")
    plt.legend(loc="upper left")
    
    plt.xlabel("Z", x=1)
    plt.ylabel("a", y=1)
    plt.xticks([-10, 0, 10])
    plt.yticks([0.5, 1])
    
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.spines['bottom'].set_position(('data', 0))
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2. tanh

    a = tanh ⁡ ( Z ) = sinh ⁡ Z cosh ⁡ Z = e Z − e − Z e x + e − Z a = \tanh(Z)=\frac{\sinh Z}{\cosh Z}=\frac{e^Z-e^{-Z}}{e^x+e^{-Z}} a=tanh(Z)=coshZsinhZ=ex+eZeZeZ ,值域介于+1和-1之间,斜率[0, 1),并且使得数据的平均值更接近0而不是0.5

    tanh

    import numpy as np
    import matplotlib.pyplot as plt
    
    Z = np.linspace(-5, 5, 100)
    a = np.tanh(Z)
    
    fig = plt.figure()
    plt.plot(Z, a, color="blue", linewidth=1, linestyle="-", label="tanh")
    plt.legend(loc="upper left")
    
    plt.xlabel("Z", x=1)
    plt.ylabel("a", y=1)
    plt.xticks([-5, 5])
    plt.yticks([-1, 1])
    
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.spines['bottom'].set_position(('data', 0))
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3. relu

    修正线性单元的函数(ReLu): a = m a x ( 0 , Z ) a = max(0, Z) a=max(0,Z) ,只要z是正值的情况下,导数恒等于1,当z是负值的时候,导数恒等于0
    relu

    import numpy as np
    import matplotlib.pyplot as plt
    
    Z = np.linspace(-5, 5, 11)
    a = np.maximum(0, Z)
    
    fig = plt.figure()
    plt.plot(Z, a, color="blue", linewidth=1, linestyle="-", label="Relu")
    plt.legend(loc="upper left")
    
    plt.xlabel("Z", x=1)
    plt.ylabel("a", y=1)
    plt.xticks([-5, 5])
    plt.yticks([5])
    
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.spines['bottom'].set_position(('data', 0))
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    4. softmax

    s o f t m a x ( Z i ) = e Z i ∑ 1 C e Z c softmax(Z_{i}) = \frac{e^{Z_{i}}}{\sum_{1}^{C}e^{Z_{c}}} softmax(Zi)=1CeZceZi,又称归一化指数函数

    它是二分类函数sigmoid在多分类上的推广,目的是将多分类的结果以概率的形式展现出来

    • 分子:通过指数函数,将实数输出映射到零到正无穷
    • 分母:将所有结果相加,进行归一化

    假如模型对一个三分类问题的预测结果为a, b, c
    softmax将差距大的数值距离拉的更大
    在深度学习中通常使用反向传播求解梯度进而使用梯度下降进行参数更新的过程,而指数函数在求导的时候比较方便
    softmax

    import numpy as np
    import matplotlib.pyplot as plt
    
    def hardmax(Z):
        return Z / sum(Z)
    
    def softmax(Z):
        return np.exp(Z) / sum(np.exp(Z))
    
    
    pred = np.array([0.5, 1.5, 4])
    a = np.array([hardmax(pred)[0], softmax(pred)[0]])
    b = np.array([hardmax(pred)[1], softmax(pred)[1]])
    c = np.array([hardmax(pred)[2], softmax(pred)[2]])
    
    x = np.arange(2)
    x_labels = ["hardmax", "softmax"]
    plt.xticks(x, x_labels)
    
    total_width, n = 0.8, 3
    width = total_width / n
    x = x - (total_width - width) / 2
    
    plt.bar(x, a, width=width, label="a")
    plt.bar(x + width, b, width=width, label="b")
    plt.bar(x + 2*width, c, width=width, label="c")
    plt.legend()
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    谢谢

  • 相关阅读:
    第二十章·中介者模式
    为什么在Java中使用Integer,1000==1000是false,而100==100是true?
    Git相关知识(1)
    python函数(8):异常处理机制+模块
    机器学习(七)朴素贝叶斯、决策树与随机森林
    Intel汇编-系统调用返回值
    HALCON支持GPU加速的算子有哪些?
    1.docker的基本使用
    极客日报:王者荣耀道歉:因新游海报擅用原神素材;Facebook改名为Meta;Node.js v16.13.0发布
    STM32自学☞DMA数据转运以及DMA+AD多通道案例
  • 原文地址:https://blog.csdn.net/qq_32618327/article/details/127412143