最全面:python绘制Sigmoid、Tanh、Swish、ELU、SELU、ReLU、ReLU6、Leaky ReLU、Mish、hard-Sigmoid、hard-Swish等激活函数(有源码)

本文提供了一个Python类,用于绘制多种激活函数及其梯度,包括Sigmoid、Swish、ReLU、Mish等,通过可视化帮助理解深度学习中不同激活函数的行为。

python绘制激活函数

代码

我构建了一个关于激活函数的类,大家可以随意使用,包括其输出值和梯度值
关于这些激活函数详解可以参考我这篇博客:深度学习—激活函数详解(Sigmoid、tanh、ReLU、ReLU6及变体P-R-Leaky、ELU、SELU、Swish、Mish、Maxout、hard-sigmoid、hard-swish)

import matplotlib.pyplot as plt
import numpy as np

class ActivateFunc():
    def __init__(self, x, b=None, lamb=None, alpha=None, a=None):
        super(ActivateFunc, self).__init__()
        self.x = x
        self.b = b
        self.lamb = lamb
        self.alpha = alpha
        self.a = a

    def Sigmoid(self):
        y = np.exp(self.x) / (np.exp(self.x) + 1)
        y_grad = y*(1-y)
        return [y, y_grad]

    def Tanh(self):
        y = np.tanh(self.x)
        y_grad = 1 - y * y
        return [y, y_grad]

    def Swish(self): #b是一个常数,指定b
        y = self.x * (np.exp(self.b*self.x) / (np.exp(self.b*self.x) + 1))
        y_grad = np.exp(self.b*self.x)/(1+np.exp(self.b*self.x)) + self.x * (self.b*np.exp(self.b*self.x) / ((1+np.exp(self.b*self.x))*(1+np.exp(self.b*self.x))))
        return [y, y_grad]

    def ELU(self): # alpha是个常数,指定alpha
        y = np.where(self.x > 0, self.x, self.alpha * (np.exp(self.x) - 1))
        y_grad = np.where(self.x > 0, 1, self.alpha * np.exp(self.x))
        return [y, y_grad]

    def SELU(self):  # lamb大于1,指定lamb和alpha
        y = np.where(self.x > 0, self.lamb * self.x, self.lamb * self.alpha * (np.exp(self.x) - 1))
        y_grad = np.where(self.x > 0, self.lamb*1, self.lamb * self.alpha * np.exp(self.x))
        return [y, y_grad]

    def ReLU(self):
        y = np.where(self.x < 0, 0, self.x)
        y_grad = np.where(self.x < 0, 0, 1)
        return [y, y_grad]

    def PReLU(self):    # a大于1,指定a
        y = np.where(self.x < 0, self.x / self.a, self.x)
        y_grad = np.where(self.x < 0, 1 / self.a, 1)
        return [y, y_grad]

    def LeakyReLU(self):   # a大于1,指定a
        y = np.where(self.x < 0, self.x / self.a, self.x)
        y_grad = np.where(self.x < 0, 1 / self.a, 1)
        return [y, y_grad]

    def Mish(self):
        f = 1 + np.exp(x)
        y = self.x * ((f*f-1) / (f*f+1))
        y_grad = (f*f-1) / (f*f+1) + self.x*(4*f*(f-1)) / ((f*f+1)*(f*f+1))
        return [y, y_grad]

    def ReLU6(self):
        y = np.where(np.where(self.x < 0, 0, self.x) > 6, 6, np.where(self.x < 0, 0, self.x))
        y_grad = np.where(self.x > 6, 0, np.where(self.x < 0, 0, 1))
        return [y, y_grad]

    def Hard_Swish(self):
        f = self.x + 3
        relu6 = np.where(np.where(f < 0, 0, f) > 6, 6, np.where(f < 0, 0, f))
        relu6_grad = np.where(f > 6, 0, np.where(f < 0, 0, 1))
        y = self.x * relu6 / 6
        y_grad = relu6 / 6 + self.x * relu6_grad / 6
        return [y, y_grad]

    def Hard_Sigmoid(self):
        f = (2 * self.x + 5) / 10
        y = np.where(np.where(f > 1, 1, f) < 0, 0, np.where(f > 1, 1, f))
        y_grad = np.where(f > 0, np.where(f >= 1, 0, 1 / 5), 0)
        return [y, y_grad]


def PlotActiFunc(x, y, title):
    plt.grid(which='minor', alpha=0.2)
    plt.grid(which='major', alpha=0.5)
    plt.plot(x, y)
    plt.title(title)
    plt.show()

def PlotMultiFunc(x, y):
    plt.grid(which='minor', alpha=0.2)
    plt.grid(which='major', alpha=0.5)
    plt.plot(x, y)


if __name__ == '__main__':
    x = np.arange(-10, 10, 0.01)
    activateFunc = ActivateFunc(x)
    activateFunc.b = 1
    PlotActiFunc(x, activateFunc.Sigmoid()[0], title='Sigmoid')
    PlotActiFunc(x, activateFunc.Swish()[0], title='Swish')
    PlotActiFunc(x, activateFunc.ReLU()[0], title='ReLU')
    PlotActiFunc(x, activateFunc.Mish()[0], title='Mish')
    PlotActiFunc(x, activateFunc.Mish()[1], title='Mish-grad')
    PlotActiFunc(x, activateFunc.Swish()[1], title='Swish-grad')

    plt.figure(1)
    PlotMultiFunc(x, activateFunc.Mish()[1])
    PlotMultiFunc(x, activateFunc.Swish()[1])
    plt.legend(['Mish-grad', 'Swish-grad'])
    plt.figure(2)

    PlotMultiFunc(x, activateFunc.Swish()[0])
    PlotMultiFunc(x, activateFunc.Mish()[0])
    plt.legend(['Swish', 'Mish'])

    plt.figure(3)
    PlotMultiFunc(x, activateFunc.Swish()[0])
    PlotMultiFunc(x, activateFunc.Hard_Swish()[0])
    plt.legend(['Swish', 'Hard-Swish'])

    plt.figure(4)
    PlotMultiFunc(x, activateFunc.Sigmoid()[0])
    PlotMultiFunc(x, activateFunc.Hard_Sigmoid()[0])
    plt.legend(['Sigmoid', 'Hard-Sigmoid'])

    plt.figure(5)
    PlotMultiFunc(x, activateFunc.ReLU()[0])
    PlotMultiFunc(x, activateFunc.ReLU6()[0])
    plt.legend(['ReLU', 'ReLU6'])

    plt.figure(6)
    PlotMultiFunc(x, activateFunc.Swish()[1])
    PlotMultiFunc(x, activateFunc.Hard_Swish()[1])
    plt.legend(['Swish-grad', 'Hard-Swish-grad'])

    plt.figure(7)
    PlotMultiFunc(x, activateFunc.Sigmoid()[1])
    PlotMultiFunc(x, activateFunc.Hard_Sigmoid()[1])
    plt.legend(['Sigmoid-grad', 'Hard-Sigmoid-grad'])

    plt.figure(8)
    PlotMultiFunc(x, activateFunc.ReLU()[1])
    PlotMultiFunc(x, activateFunc.ReLU6()[1])
    plt.legend(['ReLU-grad', 'ReLU6-grad'])

    plt.show()

示例

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

加粗样式

在这里插入图片描述


2019.10.30
希望能帮到你。


<think>好的,我现在需要帮助用户在MATLAB中绘制sigmoidtanhReLULeakyReLU激活函数的图像。首先,我得回忆一下这些激活函数的数学表达式,然后考虑如何在MATLAB中实现它们。 首先,用户提到了四个激活函数sigmoidtanhReLULeakyReLU。我需要逐一确认每个函数的定义。比如,sigmoid函数通常是$f(x) = \frac{1}{1 + e^{-x}}$,tanh是双曲正切函数,表达式为$f(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}$,或者也可以用双曲函数库里的函数。ReLU比较简单,就是$f(x) = \max(0, x)$,而LeakyReLU则是当x小于0时,乘以一个小的斜率,比如0.01,所以表达式是$f(x) = \begin{cases} x & \text{if } x \geq 0 \\ 0.01x & \text{otherwise} \end{cases}$。 接下来,考虑如何在MATLAB中实现这些函数。对于sigmoid,可能需要用元素级运算,比如用exp函数计算指数。同样,tanh在MATLAB中可能有内置函数,但为了准确起见,可能需要手动实现或者调用正确的函数。ReLULeakyReLU可以用条件判断,或者更高效的方式,比如逐元素比较。 然后,关于绘图部分。用户希望将这四个函数绘制出来。在MATLAB中,通常需要生成一个x值的向量,然后计算对应的y值,再使用plot函数绘制。可能需要设置不同的颜色和线条样式来区分各个激活函数。同时,添加网格、标签、标题和图例也是必要的步骤,以便图像更清晰。 需要注意的几点:首先,生成x的范围,比如从-5到5,步长0.1,这样可以覆盖函数的典型变化区域。其次,处理每个函数时,确保数组运算正确,避免维度错误。例如,sigmoid函数中的指数运算应该是对每个元素进行操作,而不是矩阵运算。可能需要使用点运算符(如.*和.^)来确保按元素计算。 对于LeakyReLU,可能需要使用逻辑索引来处理x中大于等于0和小于0的部分。例如,在MATLAB中,可以这样实现:y = x .* (x >= 0) + 0.01 * x .* (x < 0)。或者使用max函数结合不同的条件。 另外,用户提到在Python中使用detach()来切断计算图,但在MATLAB中不需要处理张量计算图的问题,因为MATLAB主要用于数值计算而不是深度学习框架中的自动微分。因此,这部分可以忽略,直接处理普通的数值数组即可。 接下来,需要整合这些步骤到一个MATLAB脚本中。可能需要分几个子图,或者在同一图中用不同颜色绘制,但考虑到四个函数放在同一图中可能比较拥挤,所以分四个子图更合适,每个子图显示一个函数。 然后,检查代码的正确性。例如,sigmoid函数是否计算正确,tanh是否有内置函数或者是否需要手动实现。在MATLAB中,tanh函数是内置的,可以直接使用。ReLU可以用max函数,LeakyReLU可能需要逻辑索引。 后,确保图像的展示效果良好,包括坐标轴标签、标题、图例、网格线等。可能需要使用hold on来在同一图中绘制多个函数,但如果是分四个子图的话,每个子图独立显示一个函数更清晰。 现在,我需要将上述思考转化为具体的MATLAB代码步骤,并确保每个激活函数的正确性,以及绘图的正确设置。同时,需要按照用户的要求,使用中文回答,并生成相关问题。后,根据用户提供的引用,检查是否有需要引用的地方,比如激活函数的定义或绘图方法是否来自参考内容。但用户提供的引用主要是关于Python的实现,而这里的问题是关于MATLAB的,因此可能引用部分不适用,但需要确认是否有相关内容需要标注。</think>### 分步实现方法 1. **定义输入范围** 生成从$-5$到$5$的连续输入值,步长$0.1$,用于绘制平滑曲线: ```matlab x = -5:0.1:5; ``` 2. **定义激活函数** - **Sigmoid**: $$f(x) = \frac{1}{1 + e^{-x}}$$ ```matlab sigmoid = 1 ./ (1 + exp(-x)); ``` - **Tanh**: $$f(x) = \tanh(x)$$(直接使用MATLAB内置函数) ```matlab tanh_y = tanh(x); ``` - **ReLU**: $$f(x) = \max(0, x)$$ ```matlab relu = max(0, x); ``` - **LeakyReLU**: $$f(x) = \begin{cases} x & x \geq 0 \\ 0.01x & x < 0 \end{cases}$$ ```matlab leakyrelu = x .* (x >= 0) + 0.01 * x .* (x < 0); ``` 3. **绘制图像** 使用`subplot`将四个函数绘制在同一窗口的不同子图中: ```matlab figure; % Sigmoid subplot(2,2,1); plot(x, sigmoid, 'b', 'LineWidth', 2); title('Sigmoid'); grid on; % Tanh subplot(2,2,2); plot(x, tanh_y, 'r', 'LineWidth', 2); title('Tanh'); grid on; % ReLU subplot(2,2,3); plot(x, relu, 'g', 'LineWidth', 2); title('ReLU'); grid on; % LeakyReLU subplot(2,2,4); plot(x, leakyrelu, 'm', 'LineWidth', 2); title('LeakyReLU'); grid on; ``` ### 完整代码 ```matlab x = -5:0.1:5; % Sigmoid sigmoid = 1 ./ (1 + exp(-x)); % Tanh tanh_y = tanh(x); % ReLU relu = max(0, x); % LeakyReLU leakyrelu = x .* (x >= 0) + 0.01 * x .* (x < 0); % 绘图 figure; subplot(2,2,1); plot(x, sigmoid, 'b', 'LineWidth', 2); title('Sigmoid'); grid on; subplot(2,2,2); plot(x, tanh_y, 'r', 'LineWidth', 2); title('Tanh'); grid on; subplot(2,2,3); plot(x, relu, 'g', 'LineWidth', 2); title('ReLU'); grid on; subplot(2,2,4); plot(x, leakyrelu, 'm', 'LineWidth', 2); title('LeakyReLU'); grid on; ``` ### 效果说明 - **Sigmoid** 输出范围$(0,1)$,适用于概率映射[^1]。 - **Tanh** 输出范围$(-1,1)$,中心对称性有助于收敛[^2]。 - **ReLU** 在$x>0$时线性增长,避免梯度消失。 - **LeakyReLU** 改进ReLU的“神经元死亡”问题,允许负值区域有微小梯度---
评论 9
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值