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爬蟲之驗(yàn)證碼篇3-滑動(dòng)驗(yàn)證碼識(shí)別技術(shù)
本篇涉及到的驗(yàn)證碼為滑動(dòng)驗(yàn)證碼,不同于極驗(yàn)證,本驗(yàn)證碼難度略低,需要的將滑塊拖動(dòng)到矩形區(qū)域右側(cè)即可完成。對(duì)python爬蟲滑動(dòng)驗(yàn)證碼識(shí)別技術(shù)感興趣的朋友跟隨小編一起看看吧2019-04-04
利用Python實(shí)現(xiàn)在同一網(wǎng)絡(luò)中的本地文件共享方法
今天小編就為大家分享一篇利用Python實(shí)現(xiàn)在同一網(wǎng)絡(luò)中的本地文件共享方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Python實(shí)現(xiàn)二值掩膜影像去噪與邊緣強(qiáng)化方法詳解
這篇文章主要介紹了Python實(shí)現(xiàn)二值掩膜影像去噪與邊緣強(qiáng)化方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
Python編程中運(yùn)用閉包時(shí)所需要注意的一些地方
這篇文章主要介紹了Python編程中運(yùn)用閉包時(shí)所需要注意的一些地方,文章來(lái)自國(guó)內(nèi)知名的Python開發(fā)者felinx的博客,需要的朋友可以參考下2015-05-05
Python實(shí)現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享
今天小編就為大家分享一篇Python實(shí)現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
python相對(duì)企業(yè)語(yǔ)言優(yōu)勢(shì)在哪
在本篇文章里小編給大家分享的是關(guān)于python相對(duì)企業(yè)語(yǔ)言優(yōu)勢(shì)以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以參考下。2020-06-06

