当前位置:   article > 正文

常用激活函数及偏导_激活 函数 偏导

激活 函数 偏导

常用激活函数及偏导

在这里插入图片描述

derivative.py


import numpy as np
import matplotlib.pyplot as plt

plt.subplots_adjust(hspace=0.5 , wspace=0.5)
rows = 3
cols = 2

def plot_style(ax):
    # 设置轴隐藏
    ax.spines['top'].set_visible(False) 
    ax.spines['right'].set_visible(False)
    # 设置轴位置
    ax.spines['bottom'].set_position(('axes', 0.5))
    ax.spines['left'].set_position(('axes', 0.5))
    # 设置刻度间隔
    # ax.xaxis.set_major_locator(plt.MultipleLocator(1))
    # ax.yaxis.set_major_locator(plt.MultipleLocator(1))
    # 设置刻度范围
    plt.xlim(-10,10)
    plt.ylim(-2,2)

# --------------------------------------

x=np.linspace(-10,10,100)

def Sigmoid(x) :
    a = 1/(1+np.exp(-x))
    return a

def d_Sigmoid(x):
    a = Sigmoid(x)*(1-Sigmoid(x))
    return a

graph = plt.subplot(rows,cols,1)
plot_style(graph)
plt.plot(x,Sigmoid(x),label="σ(x)")
plt.plot(x,d_Sigmoid(x),label="σ`(x)")
# plt.title('σ(x) & σ`(x)=σ(1-σ)')
plt.legend()

# plt.show()

# --------------------------------------
def LeakyReLU(x,p):
    cond = x>=0
    a = np.where(cond,x,p*x) 
    return a 
def d_LeakyReLU(x,p):
    cond = x>=0
    a = np.where(cond,1,p) 
    return a 

x=np.linspace(-10,10,100)
graph = plt.subplot(rows,cols,2)
plot_style(graph)
p = 0.2
plt.plot(x,LeakyReLU(x,p),label='LeakyReLU(x)')
plt.plot(x,d_LeakyReLU(x,p),label='`LeakyReLU(x)')
plt.title(f'p={p}')
plt.legend()

# --------------------------------------
def ReLU(x) :
    a = LeakyReLU(x,0)
    return a 

def d_ReLU(x) :
    a = np.array(x,copy=True)
    a[x<0] = 0
    a[x>=0] = 1
    return a 

x=np.linspace(-10,10,100)
graph = plt.subplot(rows,cols,3)
plot_style(graph)
plt.plot(x,ReLU(x),label='ReLU(x)')
plt.plot(x,d_ReLU(x),label='`ReLU(x)')
# plt.title('ReLU(x) & `ReLU(x)')
plt.legend()
# --------------------------------------


def tanh(x):
    a = 2*Sigmoid(2*x) - 1
    return a

def d_tanh(x):
    b = np.power(tanh(x),2) 
    a = 1 - b
    return a

x=np.linspace(-10,10,100)
graph = plt.subplot(rows,cols,4)
plot_style(graph)
plt.plot(x,tanh(x),label='tanh(x)')
plt.plot(x,d_tanh(x),label='`tanh(x)')
# plt.title(' tanh(x) & `tanh(x) ')
plt.legend()
# --------------------------------------

# x=np.linspace(0,10,3)
x = [2,3,4]
def softmax(x):
    expx = np.exp(x)
    a = expx / np.sum(expx)
    return a

def d_softmax(x):
    p = softmax(x)
    count = len(x)
    out = []
    for i in range(count) :
        for j in range(count) :
            ret = 0
            if j==i:
                ret = p[i]*(1-p[j])
                print(f'dp[{i}]/dz[{j}] = p[{i}]*(1-p[{j})] = {ret}')
            else :
                ret = -p[i]*p[j]
                print(f'dp[{i}]/dz[{j}] = -p[{i}]*p[{j}] = {ret}')
            out.append(ret)
    return out

plt.subplot(rows,cols,5)
plt.scatter(x,softmax(x),label=f'softmax(zi)')
d_out = d_softmax(x)
len = len(x)
for i in range(len):
    plt.scatter(x,d_out[len*i:len*(i+1)],label=f'`softmax(z{i})')

plt.legend(loc='upper left', bbox_to_anchor=(1, 1))

# --------------------------------------

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
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/648933
推荐阅读
相关标签
  

闽ICP备14008679号