本文實(shí)例為大家分享了pytorch繪制曲線的具體代碼,供大家參考,具體內(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)表示將整個(gè)圖像窗口分為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那個(gè) #圖例是集中于地圖一角或一側(cè)的地圖上各種符號(hào)和顏色所代表內(nèi)容與指標(biāo)的說明,有助于更好的認(rèn)識(shí)地圖 plt.subplot(222)# 使用plt.subplot來創(chuàng)建小圖. plt.subplot(221)表示將整個(gè)圖像窗口分為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那個(gè) #圖例是集中于地圖一角或一側(cè)的地圖上各種符號(hào)和顏色所代表內(nèi)容與指標(biāo)的說明,有助于更好的認(rèn)識(shí)地圖 plt.subplot(223)# 使用plt.subplot來創(chuàng)建小圖. plt.subplot(221)表示將整個(gè)圖像窗口分為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那個(gè) #圖例是集中于地圖一角或一側(cè)的地圖上各種符號(hào)和顏色所代表內(nèi)容與指標(biāo)的說明,有助于更好的認(rèn)識(shí)地圖 plt.subplot(224)# 使用plt.subplot來創(chuàng)建小圖. plt.subplot(221)表示將整個(gè)圖像窗口分為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那個(gè) #圖例是集中于地圖一角或一側(cè)的地圖上各種符號(hào)和顏色所代表內(nèi)容與指標(biāo)的說明,有助于更好的認(rèn)識(shí)地圖 plt.show() #plt.show()則是將plt.imshow()處理后的函數(shù)顯示出來。
運(yùn)行結(jié)果:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:石家莊 浙江 文山 錫林郭勒盟 西寧 梅州 昆明 懷化
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytorch繪制曲線的方法》,本文關(guān)鍵詞 pytorch,繪制,曲線,的,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。