栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用Python画激活函数图像

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Python画激活函数图像

import numpy as np
import matplotlib.pyplot as plt

# 0 设置字体
plt.rc('font',family='Times New Roman', size=15)

# 1.1 定义sigmoid函数
def sigmoid(x):
    return 1. / (1 + np.exp(-x))
# 1.2 定义tanh函数
def tanh(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
# 1.3 定义relu函数
def relu(x):
    return np.where(x < 0, 0, x)
def Leaky_relu(x):
    return np.maximum(0.01*x,x)

# 2.1 定义绘制函数sigmoid函数
def plot_Sigmoid(fig):
    x = np.arange(-10, 10, 0.1)
    y = sigmoid(x)
    # fig = plt.figure()
    ax = fig.add_subplot(221)
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.plot(x, y,color="blue", lw=3)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.xlim([-10.05, 10.05])
    plt.ylim([-0.02, 1.02])
    plt.tight_layout()
    plt.title("Sigmoid")
    # plt.show()
# 2.2 定义绘制函数tanh函数
def plot_Tanh(fig):
    x = np.arange(-10, 10, 0.1)
    y = tanh(x)
    # fig = plt.figure()
    ax = fig.add_subplot(222)
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.spines['bottom'].set_position(('data', 0))
    ax.plot(x, y, color="blue", lw=3)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.xlim([-10.05, 10.05])
    plt.ylim([-0.02, 1.02])
    ax.set_yticks([-1.0, -0.5, 0.5, 1.0])
    ax.set_xticks([-10, -5, 5, 10])
    plt.tight_layout()
    plt.title("Tanh")
    # plt.show()
# 2.3 定义绘制函数relu函数
def plot_Relu(fig):
    x = np.arange(-10, 10, 0.1)
    y = relu(x)
    # fig = plt.figure()
    ax = fig.add_subplot(223)
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.plot(x, y, color="blue", lw=3)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.xlim([-10.05, 10.05])
    plt.ylim([-0.02, 1.02])
    ax.set_yticks([2, 4, 6, 8, 10])
    plt.tight_layout()
    plt.title("Relu")
    # plt.show()
# 2.4 定义绘制函数Lrelu函数
def plot_Lrelu(fig):
    x = np.arange(-10, 10, 0.1)
    y =Leaky_relu(x)
    # fig = plt.figure()
    ax = fig.add_subplot(224)
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.spines['bottom'].set_position(('data', 0))
    ax.plot(x, y, color="blue", lw=3)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.tight_layout()
    plt.title("Leaky_relu")
    plt.show()


# 3 运行程序
fig = plt.figure()
plot_Sigmoid(fig)
plot_Tanh(fig)
plot_Relu(fig)
plot_Lrelu(fig)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921016.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号