plt画图(sigmoid、relu、softmax)

简介: plt画图(sigmoid、relu、softmax)

代码(总)

# -*- coding:utf-8 -*-
from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.axisartist as axisartist
from matplotlib.pyplot import MultipleLocator
def sigmoid(x):
    return 1. / (1 + np.exp(-x))
def tanh(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
def relu(x):
    return np.where(x < 0, 0, x)
def prelu(x):
    return np.where(x < 0, 0.5 * x, x)
def sigmoid(x):
    #直接返回sigmoid函数
    return 1. / (1. + np.exp(-x))
def softmax(x):
    return np.exp(x)/np.sum(np.exp(x), axis=0)
def plot_sigmoid():
    # param:起点,终点,间距
    x = np.arange(-10, 10, 0.5)
    print(x)
    y = sigmoid(x)
    #plt.plot(x, label='sigmoid')  # 添加label设置图例名称
    #plt.title("sigmoid",fontsize=20)
    plt.grid()
    plt.plot(x, y,label='sigmoid',color='r')
    plt.legend(fontsize=20)
    plt.xlabel("x",fontsize=20)
    plt.ylabel("f(x)",fontsize=20)
    # 设置刻度字体大小
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlim([-10, 10])
    plt.ylim([0, 1])
    x_major_locator = MultipleLocator(5)
    # 把x轴的刻度间隔设置为1,并存在变量里
    y_major_locator = MultipleLocator(0.2)
    # 把y轴的刻度间隔设置为10,并存在变量里
    ax = plt.gca()
    # ax为两条坐标轴的实例
    ax.xaxis.set_major_locator(x_major_locator)
    # 把x轴的主刻度设置为1的倍数
    ax.yaxis.set_major_locator(y_major_locator)
    # 把y轴的主刻度设置为10的倍数
    plt.show()
def plot_tanh():
    x = np.arange(-10, 10, 0.1)
    y = tanh(x)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    # ax.spines['bottom'].set_color('none')
    # ax.spines['left'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.spines['bottom'].set_position(('data', 0))
    ax.plot(x, y)
    plt.xlim([-10.05, 10.05])
    plt.ylim([-1.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.savefig("tanh.png")
    plt.show()
def plot_relu():
    x = np.arange(-10, 10, 0.1)
    y = relu(x)
    # fig = plt.figure()
    # ax = fig.add_subplot(111)
    # ax.spines['top'].set_color('none')
    # ax.spines['right'].set_color('none')
    # # ax.spines['bottom'].set_color('none')
    # # ax.spines['left'].set_color('none')
    # ax.spines['left'].set_position(('data', 0))
    # ax.plot(x, y)
    # plt.xlim([-10.05, 10.05])
    # plt.ylim([0, 10.02])
    # ax.set_yticks([2, 4, 6, 8, 10])
    # plt.tight_layout()
    # plt.savefig("relu.png")
    #plt.title("relu")
    plt.grid()
    plt.plot(x, y, label='relu', color='r')
    plt.legend(fontsize=20)
    plt.xlabel("x",fontsize=20)
    plt.ylabel("f(x)",fontsize=20)
    # 设置刻度字体大小
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlim([-10, 10])
    plt.ylim([0, 10])
    x_major_locator = MultipleLocator(5)
    # 把x轴的刻度间隔设置为1,并存在变量里
    y_major_locator = MultipleLocator(5)
    # 把y轴的刻度间隔设置为10,并存在变量里
    ax = plt.gca()
    # ax为两条坐标轴的实例
    ax.xaxis.set_major_locator(x_major_locator)
    # 把x轴的主刻度设置为1的倍数
    ax.yaxis.set_major_locator(y_major_locator)
    # 把y轴的主刻度设置为10的倍数
    plt.show()
def plot_prelu():
    x = np.arange(-10, 10, 0.1)
    y = prelu(x)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    # ax.spines['bottom'].set_color('none')
    # ax.spines['left'].set_color('none')
    ax.spines['left'].set_position(('data', 0))
    ax.spines['bottom'].set_position(('data', 0))
    ax.plot(x, y)
    plt.xticks([])
    plt.yticks([])
    plt.tight_layout()
    plt.savefig("prelu.png")
    plt.show()
def plot_softmax():
    x = np.arange(-10, 10, 0.1)
    y = softmax(x)
    # fig = plt.figure()
    # ax = fig.add_subplot(111)
    # ax.spines['top'].set_color('none')
    # ax.spines['right'].set_color('none')
    # # ax.spines['bottom'].set_color('none')
    # # ax.spines['left'].set_color('none')
    # ax.spines['left'].set_position(('data', 0))
    # ax.plot(x, y)
    # plt.xlim([-10.05, 10.05])
    # plt.ylim([0, 10.02])
    # ax.set_yticks([2, 4, 6, 8, 10])
    # plt.tight_layout()
    # plt.savefig("relu.png")
    #plt.title("softmax")
    plt.grid()
    plt.plot(x, y, label='softmax', color='r')
    plt.legend(fontsize=20)
    plt.xlabel("x",fontsize=20)
    plt.ylabel("f(x)",fontsize=20)
    plt.xlim([-10, 10])
    plt.ylim([0, 0.1])
    # 设置刻度字体大小
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    x_major_locator = MultipleLocator(5)
    # 把x轴的刻度间隔设置为1,并存在变量里
    y_major_locator = MultipleLocator(0.02)
    # 把y轴的刻度间隔设置为10,并存在变量里
    ax = plt.gca()
    # ax为两条坐标轴的实例
    ax.xaxis.set_major_locator(x_major_locator)
    # 把x轴的主刻度设置为1的倍数
    ax.yaxis.set_major_locator(y_major_locator)
    # 把y轴的主刻度设置为10的倍数
    plt.show()
if __name__ == "__main__":
    #plot_sigmoid()
    #plot_tanh()
    #plot_relu()
    plot_softmax()
    #plot_prelu()
目录
相关文章
|
5月前
|
机器学习/深度学习 PyTorch 算法框架/工具
PyTorch基础之激活函数模块中Sigmoid、Tanh、ReLU、LeakyReLU函数讲解(附源码)
PyTorch基础之激活函数模块中Sigmoid、Tanh、ReLU、LeakyReLU函数讲解(附源码)
65 0
|
8月前
|
机器学习/深度学习 搜索推荐 Python
L1范数(L1 norm)
L1范数(L1 norm),也称为曼哈顿距离(Manhattan distance)或绝对值范数(Absolute value norm),是向量中各个元素绝对值之和。它在数学和机器学习中经常被用作一种正则化项或稀疏性度量。
509 2
|
9月前
|
机器学习/深度学习 搜索推荐 Python
L2范数(L2 norm)
L2范数(L2 norm),也称为欧几里德范数(Euclidean norm)或2-范数,是向量元素的平方和的平方根。它在数学和机器学习中经常被用作一种正则化项、距离度量或误差度量。
3774 1
|
9月前
|
机器学习/深度学习 算法 搜索推荐
L0范数(L0 norm)
L0范数(L0 norm)是指向量中非零元素的个数。与L1范数和L2范数不同,L0范数并不是一种常见的范数形式,它更多地被用作一种表示稀疏性的度量。
304 1
|
9月前
|
机器学习/深度学习 PyTorch TensorFlow
张量(Tensor)
张量(Tensor)是矩阵的推广,是一种多维数组或多维矩阵的概念。它可以包含零个或多个轴(也称为维度),每个轴上有固定的大小。张量可以是标量(零维张量)、向量(一维张量)、矩阵(二维张量)以及更高维度的数组。
93 1
|
9月前
|
机器学习/深度学习 算法 PyTorch
Softmax回归(Softmax Regression)
Softmax回归(Softmax Regression),也称为多类别逻辑回归或多项式回归,是一种用于解决多类别分类问题的统计学习方法。它是逻辑回归在多类别情况下的扩展。
145 3
|
机器学习/深度学习 PyTorch 算法框架/工具
pytorch中nn.ReLU()和F.relu()有什么区别?
pytorch中nn.ReLU()和F.relu()有什么区别?
372 0
|
11月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【PyTorch】nn.ReLU()与F.relu()的区别
【PyTorch】nn.ReLU()与F.relu()的区别
99 0
|
12月前
|
机器学习/深度学习 人工智能 自然语言处理
【Pytorch神经网络理论篇】 07 激活函数+Sigmoid+tanh+ReLU+Swish+Mish+GELU
对于分类任务来说,如果仅仅给出分类的结果,在某些场景下,提供的信息可能并不充足,这就会带来一定的局限。因此,我们建立分类模型,不仅应该能够进行分类,同时,也应该能够提供样本属于该类别的概率。这在现实中是非常实用的。例如,某人患病的概率,明天下雨概率等。因此,我们需要将z的值转换为概率值,逻辑回归使用sigmoid函数来实现转换。
441 0
|
机器学习/深度学习 资源调度
深度学习基础入门篇[四]:激活函数介绍:tanh、sigmoid、ReLU、PReLU、ELU、softplus、softmax、swish等
深度学习基础入门篇[四]:激活函数介绍:tanh、sigmoid、ReLU、PReLU、ELU、softplus、softmax、swish等
深度学习基础入门篇[四]:激活函数介绍:tanh、sigmoid、ReLU、PReLU、ELU、softplus、softmax、swish等