绘制神经元网络中常见激活函数的图形

  • sigmod函数
  • tanh函数
  • ReLU函数
  • softplus函数
  • 绘制swish函数图形,绘制当\beta = [0, 0.5, 1, 100]时的图形。

代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/11/24 15:42
# @Author  : Cai
# @Site    : 
# @File    : 1.py
# @Software: PyCharm

from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.axisartist as axisartist


def sigmoid():
    nums = np.arange(-10, 10, step=1)  # 生成一个numpy数组
    # ax = plt.subplots(figsize=(12, 4))  # 绘制子图
    ax = fig.add_subplot(1, 2, 1)
    # ax.set(title="sigmoid", xlabel="x", ylabel="y=sigmoid(x)")
    ax.plot(nums, 1.0 / (1 + np.exp(-nums)), 'r--')  # 绘制sigmoid的函数图像,r代表红色,--,..,-.,marker可以选择o^v


def tanh():
    # 采样
    x = np.linspace(-10, 10, 10000)
    y = (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))

    ax = fig.add_subplot(1, 2, 1)
    # ax.set(title="tanh", xlabel="x", ylabel="y=tanh(x)")
    # 设置线宽和颜色
    ax.plot(x, y, linewidth=3.0, color="blue", linestyle='-')


def relu():
    # 采样
    x = np.linspace(-10, 10, 10000)
    y = np.zeros(shape=(x.shape[0]), dtype=np.float)
    for i, v in enumerate(x):
        if v > 0:
            y[i] = v
        else:
            y[i] = 0
    # fig = plt.figure()
    ax = fig.add_subplot(1, 2, 1)
    # ax.set(title="Relu", xlabel="x", ylabel="y=Relu(x)")
    # 设置线宽和颜色
    ax.plot(x, y, linewidth=3.0, color="yellow")


def softplus():
    # 采样
    x = np.linspace(-10, 10, 100)
    y = np.log(1 + np.exp(x))

    ax = fig.add_subplot(2, 6, 1)
    # ax.set(title="tanh", xlabel="x", ylabel="y=tanh(x)")
    # 设置线宽和颜色
    ax.plot(x, y, linewidth=3.0, color="purple", linestyle='-')


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 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 PlotActiFunc(x, y):
    plt.grid(which='minor', alpha=0.2)
    plt.grid(which='major', alpha=0.5)
    plt.plot(x, y)


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

fig = plt.figure()
sigmoid()
tanh()
relu()
softplus()
plt.show()

x = np.arange(-10, 10, 0.01)
activateFunc = ActivateFunc(x)
activateFunc.b = 0
PlotActiFunc(x, activateFunc.Swish()[0])
activateFunc.b = 0.5
PlotActiFunc(x, activateFunc.Swish()[0])
activateFunc.b = 1
PlotActiFunc(x, activateFunc.Swish()[0])
activateFunc.b = 100
PlotActiFunc(x, activateFunc.Swish()[0])
plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值