人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

python繪制lost損失曲線加方差范圍的操作方法

發(fā)布日期:2021-12-27 10:45 | 文章來源:源碼之家

1. 導(dǎo)入必要的包

我使用了seaborn,通過sns.set_style可以讓繪制出來的圖更漂亮,而且可以切換不同的類型

import re
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import shutil
import os
sns.set_style('whitegrid')

2. 數(shù)據(jù)的獲取(可跳過此步)

我用的數(shù)據(jù)是通過深度強化得到的回報曲線。數(shù)據(jù)結(jié)構(gòu)如下所示,我所需要的是從train開始的部分,分別對應(yīng)總的回報,平均回報和回報的方差。我采用了re.findall的正則表達式去提取我所需要的數(shù)據(jù),具體的操作方式可以查看源碼。

10-15 22:23:15 DATA/traffic DEBUG train 0 totalreward : -99477.0 ReturnAvg : -102.55360824742269 ReturnStd : 34.34301970480272
10-15 22:23:29 DATA/traffic DEBUG train 1 totalreward : -83131.0 ReturnAvg : -85.70206185567011 ReturnStd : 53.442993000985545

file_path = 'log.txt'
content = []
with open(file_path, 'r') as f:
 for line in f.readlines():
  line = line.strip('\n')
  content.append(line)
iter = []
totalreward = []
returnavg = []
returnstd = []
for line in content:
 str1 = re.findall('train.+', line)
 v = [float(x) for x in re.findall('-?\d+.?\d+|\d+', str1[0])]
 iter.append(v[0])
 totalreward.append(v[1])
 returnavg.append(v[2])
 returnstd.append(v[3])

3. 回報繪制

直接將圖像保存到Plot的文件夾,這里保存不了jpg格式,一直保存,最后將其保存為png格式成功。設(shè)置分辨率為1000,其實差不多,只是線更清楚了。

color = cm.viridis(0.5)
f, ax = plt.subplots(1,1)
ax.plot(iter, totalreward, color=color)
ax.legend()
ax.set_xlabel('Iteration')
ax.set_ylabel('Return')
exp_dir = 'Plot/'
if not os.path.exists(exp_dir):
 os.makedirs(exp_dir, exist_ok=True)
else:
 os.makedirs(exp_dir, exist_ok=True)
f.savefig(os.path.join('Plot', 'reward' + '.png'), dpi=1000)

曲線如下圖,可通過plt.show()顯示出來,或者直接在console輸入f并回車

4.含有方差的平均回報繪制

在強化學習的論文中,我們經(jīng)常看到一條收斂線,周圍還有淺淺的范圍線,那些范圍線就是方差。繪制代碼如下,主要包含了fill_between.

color = cm.viridis(0.7)
f, ax = plt.subplots(1,1)
ax.plot(iter, returnavg, color=color)
r1 = list(map(lambda x: x[0]-x[1], zip(returnavg, returnstd)))
r2 = list(map(lambda x: x[0]+x[1], zip(returnavg, returnstd)))
ax.fill_between(iter, r1, r2, color=color, alpha=0.2)
ax.legend()
ax.set_xlabel('Iteration')
ax.set_ylabel('Return')
exp_dir = 'Plot/'
if not os.path.exists(exp_dir):
 os.makedirs(exp_dir, exist_ok=True)
f.savefig(os.path.join('Plot', 'avgreward' + '.png'), dpi=50)

結(jié)果如下

可以看到深綠色上下包裹著淺綠色的線,這就是fill_between的作用,其中可以調(diào)節(jié)alpha來改變顏色深度。

到此這篇關(guān)于python繪制lost損失曲線加方差范圍的文章就介紹到這了,更多相關(guān)python損失曲線 內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部