pytorch5:pytorch常用激活函数图像绘制

记录一下

import numpy as np
import math
import matplotlib.pyplot as plt


def sigmod():
    x = np.arange(-10, 10, 0.1)
    print(x)

    y = []
    for t in x:
        y_1 = 1 / (1 + math.exp(-t))
        y.append(y_1)
    print(y)
    plt.plot(x, y, label="sigmoid")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.ylim(0, 1)#设置横纵坐标轴范围
    plt.legend()
    plt.show()

在这里插入图片描述

def Tanh():
    x = np.arange(-5, 5, 0.1)
    print(x)
    y = []
    for t in x:
        y_1 =  (1 - math.exp(-(2*t))) / (1 + math.exp(-(2*t)))
        y.append(y_1)
    print(y)
    plt.plot(x, y, label="Tanh")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.ylim(-1, 1)#设置横纵坐标轴范围
    plt.legend()
    plt.show()

在这里插入图片描述

def relu():
    # 函数
    g = lambda z: np.maximum(0, z)

    start = -10  # 输入需要绘制的起始值(从左到右)
    stop = 10  # 输入需要绘制的终点值
    step = 0.01  # 输入步长
    num = (stop - start) / step  # 计算点的个数
    x = np.linspace(start, stop, int(num))
    y = g(x)

    #fig = plt.figure(1)
    plt.plot(x, y, label='relu')
    plt.grid(False)  # 显示网格

    plt.legend()  # 显示旁注
    # plt.show(fig)
    plt.show()

在这里插入图片描述

def leaky_relu():
   g = lambda z: np.maximum(0.01 * z, z)

   start = -150  # 输入需要绘制的起始值(从左到右)
   stop = 50  # 输入需要绘制的终点值
   step = 0.01  # 输入步长
   num = (stop - start) / step  # 计算点的个数
   x = np.linspace(start, stop, int(num))
   y = g(x)

   fig = plt.figure(1)
   plt.plot(x, y, label='Leaky ReLU')
   plt.grid(True)  # 显示网格

   plt.legend()  # 显示旁注
   plt.show(fig)

在这里插入图片描述

def SiLU():

   x = np.arange(-10, 10, 0.1)

   y = []
   for t in x:
       y_1 = t*(1 / (1 + math.exp(-t)))
       y.append(y_1)
   print(y)
   plt.plot(x, y, label="SiLU")
   plt.xlabel("x")
   plt.ylabel("y")
   plt.ylim(-1, 10)#设置横纵坐标轴范围
   plt.grid(True)  # 显示网格
   plt.legend()
   plt.show()

在这里插入图片描述

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值