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

補(bǔ)充知識(shí):python:實(shí)現(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ù)的案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5窗口切換的堆疊布局示例詳解
本文以堆疊窗口控件為例,詳細(xì)介紹堆疊布局的界面設(shè)計(jì)和程序?qū)崿F(xiàn)過(guò)程,通過(guò)案例帶小白創(chuàng)建一個(gè)典型的堆疊布局多窗口切換程序2021-10-10
Python?Selenium無(wú)法打開(kāi)Chrome瀏覽器處理自定義瀏覽器路徑的問(wèn)題及解決方法
Python?Selenium控制Chrome瀏覽器的過(guò)程中,由于安裝的Chrome瀏覽器的版本找不到對(duì)應(yīng)版本的驅(qū)動(dòng)chromedriver.exe文件,下載了小幾個(gè)版本號(hào)的驅(qū)動(dòng)軟件都無(wú)法正常使用,下面通過(guò)本文介紹Python?Selenium無(wú)法打開(kāi)Chrome瀏覽器處理自定義瀏覽器路徑的問(wèn)題,需要的朋友可以參考下2024-08-08
快速解決jupyter notebook啟動(dòng)需要密碼的問(wèn)題
這篇文章主要介紹了快速解決jupyter notebook啟動(dòng)需要密碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
請(qǐng)不要重復(fù)犯我在學(xué)習(xí)Python和Linux系統(tǒng)上的錯(cuò)誤
本人已經(jīng)在運(yùn)維行業(yè)工作了將近十年,我最早接觸Linux是在大二的樣子,那時(shí)候只追求易懂,所以就選擇了Ubuntu作為學(xué)習(xí)、使用的對(duì)象,它簡(jiǎn)單、易用、好操作、界面絢麗,對(duì)于想接觸Linux的新手來(lái)說(shuō)是非常不錯(cuò)的2016-12-12
python將YUV420P文件轉(zhuǎn)PNG圖片格式的兩種方法
這篇文章主要介紹了python將YUV420P文件轉(zhuǎn)PNG圖片格式的兩種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
Python3操作SQL Server數(shù)據(jù)庫(kù)(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Python3操作SQL Server數(shù)據(jù)庫(kù)(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
python中while和for的區(qū)別總結(jié)
在本篇內(nèi)容里小編給大家分享的是關(guān)于python中while和for的區(qū)別以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。2019-06-06

