【基础】激活函数看这一篇就够了


常见的激活函数包括Sigmoid函数、ReLU函数、Leaky ReLU函数、Softmax函数、Tanh函数、ELU函数(指数线性单元)、Swish函数等。

一. sigmoid函数

sigmoid函数将输入的任意值映射到(0,1)区间,常用于输出层的二分类问题。

1.1常见面试知识点

sigmoid函数的输出在极端值处(接近0或1)梯度接近0,容易导致梯度消失或者梯度爆炸问题。

1.2 代码示例

import numpy as np
def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))

1.3 图示

这里写图片描述

# ================ 绘制代码 ================ 
import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))
    
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)

x = np.linspace(-10, 10)
y = sigmoid(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])

plt.scatter(x, y, label="Sigmoid", color="red")
plt.plot(x, y, label="Sigmoid", color="blue")
plt.legend()
plt.show()

二. ReLu函数

relu函数将负数值截断为0,保留正数值不变,常用于隐藏层的激活函数。

2.1常见面试知识点

relu函数存在神经元死亡问题,即负数输入时梯度为0,导致该神经元无法更新参数。

2.2 代码示例

import numpy as np
def relu(x):
    return np.where(x<0,0,x)

2.3 图示

这里写图片描述

# ================ 绘制代码 ================ 
import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
def relu(x):
    return np.where(x<0,0,x)
    
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)

x = np.linspace(-10, 10)
y = relu(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])

plt.scatter(x, y, label="Relu", color="red")
plt.plot(x, y, label="Relu", color="blue")
plt.legend()
plt.show()

三. Leaky ReLu函数

relu函数将负数值截断为0,保留正数值不变,常用于隐藏层的激活函数。

3.1常见面试知识点

由于relu函数存在神经元死亡问题,为了解决这个问题,提出Leaky ReLu函数,核心思路是通过引入一个小的斜率保证负数输入时也有梯度。

3.2 代码示例

import numpy as np
def leaky_relu(x, alpha=0.01):
    return np.where(x<0,alpha*x,x)

3.3 图示

在这里插入图片描述

# ================ 绘制代码 ================ 
import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
def leaky_relu(x, alpha=0.1):
    return np.where(x<0,alpha*x,x)
    
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)

x = np.linspace(-10, 10)
y = leaky_relu(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])

plt.scatter(x, y, label="Leaky Relu", color="red")
plt.plot(x, y, label="Leaky Relu", color="blue")
plt.legend()
plt.show()

四. Softmax函数

softmax函数常用于多分类问题的输出层,将模型的原始输出转换成概率分布。

4.1常见面试知识点

softmax函数的输出是归一化的概率分布,所有类别的概率和为1。

4.2 代码示例

import numpy as np
def softmax(x):
    exp_x = np.exp(x - np.max(x, axis=-1, keepdims=True))
    return exp_x / np.sum(exp_x, axis=-1, keepdims=True)

4.3 图示

在这里插入图片描述

# ================ 绘制代码 ================ 
import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
def softmax(x):
    exp_x = np.exp(x - np.max(x, axis=-1, keepdims=True))
    return exp_x / np.sum(exp_x, axis=-1, keepdims=True)
    
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)

x = np.linspace(-10, 10)
y = softmax(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])

plt.scatter(x, y, label="softmax", color="red")
plt.plot(x, y, label="softmax", color="blue")
plt.legend()
plt.show()

五. Tanh函数(双曲正切函数)

tanh函数将输入的任意值映射到(-1,1)区间,具有sigmoid函数的非线性特性,但输出的均值接近0。

5.1常见面试知识点

tanh函数也存在梯度消失和梯度爆炸问题,尤其在输入较大或较小的情况下。

5.2 代码示例

import numpy as np
def tanh(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))

5.3 图示

这里写图片描述

import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
def tanh(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
    
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)

x = np.linspace(-10, 10)
y = tanh(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])

plt.scatter(x, y, label="tanh", color="red")
plt.plot(x, y, label="tanh", color="blue")
plt.legend()
plt.show()

六. ELU函数(指数线性单元)

ELU函数在负数范围内具有指数增长的特性,相对于ReLU函数能够减少神经元死亡问题。

6.1常见面试知识点

ELU函数相对于ReLU函数的计算量稍大,但在某些情况下可以提供更好的训练性能。

6.2 代码示例

import numpy as np
def elu(x, alpha=1.0):
    return np.where(x > 0, x, alpha * (np.exp(x) - 1))

6.3 图示

在这里插入图片描述

import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
def elu(x, alpha=1.0):
    return np.where(x > 0, x, alpha * (np.exp(x) - 1))
    
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)

x = np.linspace(-10, 10)
y = elu(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])

plt.scatter(x, y, label="ELU", color="red")
plt.plot(x, y, label="ELU", color="blue")
plt.legend()
plt.show()

七. ELU函数(指数线性单元)

Swish函数是由谷歌提出的一种激活函数,具有ReLU函数的非线性特性,并且可以通过自动微分进行端到端的训练。

7.1常见面试知识点

Swish函数在一些实验中表现良好,但并不适用于所有情况,需要根据具体问题进行验证。

7.2 代码示例

import numpy as np
def swish(x):
    return x * sigmoid(x)

7.3 图示

在这里插入图片描述

import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))
def swish(x):
    return x * sigmoid(x)
    
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)

x = np.linspace(-10, 10)
y = swish(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])

plt.scatter(x, y, label="swish", color="red")
plt.plot(x, y, label="swish", color="blue")
plt.legend()
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值