Python3 用matplotlib繪制sigmoid函數(shù)的案例
我就廢話不多說了,大家還是直接看代碼吧~
import matplotlib.pyplot as plt import numpy as np def sigmoid(x): # 直接返回sigmoid函數(shù) return 1. / (1. + np.exp(-x)) def plot_sigmoid(): # param:起點,終點,間距 x = np.arange(-8, 8, 0.2) y = sigmoid(x) plt.plot(x, y) plt.show() if __name__ == '__main__': plot_sigmoid()
如圖:

補充知識:python:實現(xiàn)并繪制 sigmoid函數(shù),tanh函數(shù),ReLU函數(shù),PReLU函數(shù)
如下所示:
# -*- coding:utf-8 -*-
from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.axisartist as axisartist
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 plot_sigmoid():
x = np.arange(-10, 10, 0.1)
y = sigmoid(x)
fig = plt.figure()
# ax = fig.add_subplot(111)
ax = axisartist.Subplot(fig,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.axis['bottom'].set_axisline_style("-|>",size=1.5)
ax.spines['left'].set_position(('data', 0))
ax.plot(x, y)
plt.xlim([-10.05, 10.05])
plt.ylim([-0.02, 1.02])
plt.tight_layout()
plt.savefig("sigmoid.png")
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.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()
if __name__ == "__main__":
plot_sigmoid()
plot_tanh()
plot_relu()
plot_prelu()
以上這篇Python3 用matplotlib繪制sigmoid函數(shù)的案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python深度學(xué)習(xí)實戰(zhàn)PyQt5窗口切換的堆疊布局示例詳解
本文以堆疊窗口控件為例,詳細介紹堆疊布局的界面設(shè)計和程序?qū)崿F(xiàn)過程,通過案例帶小白創(chuàng)建一個典型的堆疊布局多窗口切換程序2021-10-10
Python?Selenium無法打開Chrome瀏覽器處理自定義瀏覽器路徑的問題及解決方法
Python?Selenium控制Chrome瀏覽器的過程中,由于安裝的Chrome瀏覽器的版本找不到對應(yīng)版本的驅(qū)動chromedriver.exe文件,下載了小幾個版本號的驅(qū)動軟件都無法正常使用,下面通過本文介紹Python?Selenium無法打開Chrome瀏覽器處理自定義瀏覽器路徑的問題,需要的朋友可以參考下2024-08-08
請不要重復(fù)犯我在學(xué)習(xí)Python和Linux系統(tǒng)上的錯誤
本人已經(jīng)在運維行業(yè)工作了將近十年,我最早接觸Linux是在大二的樣子,那時候只追求易懂,所以就選擇了Ubuntu作為學(xué)習(xí)、使用的對象,它簡單、易用、好操作、界面絢麗,對于想接觸Linux的新手來說是非常不錯的2016-12-12
python將YUV420P文件轉(zhuǎn)PNG圖片格式的兩種方法
這篇文章主要介紹了python將YUV420P文件轉(zhuǎn)PNG圖片格式的兩種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
Python3操作SQL Server數(shù)據(jù)庫(實例講解)
下面小編就為大家?guī)硪黄狿ython3操作SQL Server數(shù)據(jù)庫(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
python中while和for的區(qū)別總結(jié)
在本篇內(nèi)容里小編給大家分享的是關(guān)于python中while和for的區(qū)別以及相關(guān)知識點,需要的朋友們可以學(xué)習(xí)下。2019-06-06

