溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在pytorch中繪制一個曲線

發(fā)布時間:2021-03-08 11:10:22 來源:億速云 閱讀:307 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了怎么在pytorch中繪制一個曲線,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

具體內(nèi)容如下

import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt
 
# fake data
x = torch.linspace(-5, 5, 200) # x data (tensor), shape=(100, 1)
x = Variable(x) #創(chuàng)建 variable(變量),構(gòu)造神經(jīng)網(wǎng)絡(luò)要使用Variable類型
x_np = x.data.numpy() # numpy array for plotting,用于繪圖的numpy數(shù)組
 
# following are popular activation functions,以下是常用的激活函數(shù)
y_relu = torch.relu(x).data.numpy()
y_sigmoid = torch.sigmoid(x).data.numpy()
y_tanh = torch.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy() # there's no softplus in torch。torch沒有softplus
# y_softmax = torch.softmax(x, dim=0).data.numpy() softmax is a special kind of activation function, it is about probability
#softmax是一種特殊的激活函數(shù),它與概率有關(guān)
 
 
# plt to visualize these activation function
#將這些激活函數(shù)可視化
plt.figure(1, figsize=(8, 6)) # 橫坐標(biāo)與縱坐標(biāo)
plt.subplot(221)
#plt.subplot()函數(shù)用于直接指定劃分方式和位置進(jìn)行繪圖。
# 使用plt.subplot來創(chuàng)建小圖. plt.subplot(221)表示將整個圖像窗口分為2行2列, 當(dāng)前位置為1.
plt.plot(x_np, y_relu, c='red', label='relu')
#plt.plot(x,y,format_string,**kwargs)
#x軸數(shù)據(jù),y軸數(shù)據(jù),format_string控制曲線的格式字串
#format_string 由顏色字符,風(fēng)格字符,和標(biāo)記字符
plt.ylim((-1, 5)) # 設(shè)置縱坐標(biāo)的范圍
plt.legend(loc='best')#plt.legend()函數(shù)的作用是給圖像加圖例。,就左上角relu那個
#圖例是集中于地圖一角或一側(cè)的地圖上各種符號和顏色所代表內(nèi)容與指標(biāo)的說明,有助于更好的認(rèn)識地圖
 
plt.subplot(222)# 使用plt.subplot來創(chuàng)建小圖. plt.subplot(221)表示將整個圖像窗口分為2行2列, 當(dāng)前位置為2.
plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
#plt.plot(x,y,format_string,**kwargs)
#x軸數(shù)據(jù),y軸數(shù)據(jù),format_string控制曲線的格式字串
#format_string 由顏色字符,風(fēng)格字符,和標(biāo)記字符
plt.ylim((-0.2, 1.2)) # 設(shè)置縱坐標(biāo)的范圍
plt.legend(loc='best')#plt.legend()函數(shù)的作用是給圖像加圖例。,就左上角relu那個
#圖例是集中于地圖一角或一側(cè)的地圖上各種符號和顏色所代表內(nèi)容與指標(biāo)的說明,有助于更好的認(rèn)識地圖
 
plt.subplot(223)# 使用plt.subplot來創(chuàng)建小圖. plt.subplot(221)表示將整個圖像窗口分為2行2列, 當(dāng)前位置為3.
plt.plot(x_np, y_tanh, c='red', label='tanh')
#plt.plot(x,y,format_string,**kwargs)
#x軸數(shù)據(jù),y軸數(shù)據(jù),format_string控制曲線的格式字串
#format_string 由顏色字符,風(fēng)格字符,和標(biāo)記字符
plt.ylim((-1.2, 1.2))# 設(shè)置縱坐標(biāo)的范圍
plt.legend(loc='best')#plt.legend()函數(shù)的作用是給圖像加圖例。,就左上角relu那個
#圖例是集中于地圖一角或一側(cè)的地圖上各種符號和顏色所代表內(nèi)容與指標(biāo)的說明,有助于更好的認(rèn)識地圖
 
plt.subplot(224)# 使用plt.subplot來創(chuàng)建小圖. plt.subplot(221)表示將整個圖像窗口分為2行2列, 當(dāng)前位置為4.
plt.plot(x_np, y_softplus, c='red', label='softplus')
#plt.plot(x,y,format_string,**kwargs)
#x軸數(shù)據(jù),y軸數(shù)據(jù),format_string控制曲線的格式字串
#format_string 由顏色字符,風(fēng)格字符,和標(biāo)記字符
plt.ylim((-0.2, 6))# 設(shè)置縱坐標(biāo)的范圍
plt.legend(loc='best')#plt.legend()函數(shù)的作用是給圖像加圖例。,就左上角relu那個
#圖例是集中于地圖一角或一側(cè)的地圖上各種符號和顏色所代表內(nèi)容與指標(biāo)的說明,有助于更好的認(rèn)識地圖
 
plt.show()
#plt.show()則是將plt.imshow()處理后的函數(shù)顯示出來。

運(yùn)行結(jié)果:

怎么在pytorch中繪制一個曲線

上述內(nèi)容就是怎么在pytorch中繪制一個曲線,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI