1 介绍
在很多的RL论文中绘制的数据图都非常精美,使用plt绘制达不到这种效果。如SAC论文中的图。
这种图是使用seaborn模块绘制的,在openai的spiningup里面也提供了相对应的绘制模块,但是使用还是比较麻烦(对于一个不用spiningup的人来说)。幸运的是,在github上发现了一个有趣的项目rl-plotter可以很方便地绘制这种效果图。
2 用法
直接使用pip装就行
pip install rl_plotter
常用用法在训练代码中添加logger就行,自动把对应log记录到csv文件下,如
from rl_plotter.logger import Logger
logger = Logger(exp_name="your_exp_name", env_name, seed, locals())
····
logger.update(score=evaluation_score_list, total_steps=current_training_steps)
训练完毕使用rl_plotter进行可是化
rl_plotter --save --show
3 Demo
生成模拟数据
'''
生成两组带有噪音的log_t(x)的曲线
'''
from rl_plotter.logger import Logger
import random,math
a = .5
b = 1
exp_name = 'TD3'
#第一个算法的第一次实验曲线
logger = Logger(exp_name=exp_name, env_name='Humanoid-V2')
for i in range(1,10000):
coiffe = random.random()*a+b
x = math.log(i, 5) *coiffe
logger.update(score=[x], total_steps=i)
# 第一个算法的第二次实验曲线
logger = Logger(exp_name=exp_name, env_name='Humanoid-V2')
for i in range(1,10000):
coiffe = random.random()*a+b
x = math.log(i, 8)*coiffe
logger.update(score=[x], total_steps=i)
exp_name = 'SAC'
#第二个算法的第一次实验曲线
logger = Logger(exp_name=exp_name, env_name='Humanoid-V2')
for i in range(1,10000):
coiffe = random.random()*a+b
x = math.log(i, 2) *coiffe
logger.update(score=[x], total_steps=i)
# 第二个算法的第二次实验曲线
logger = Logger(exp_name=exp_name, env_name='Humanoid-V2')
for i in range(1,10000):
coiffe = random.random()*a+b
x = math.log(i, 4)*coiffe
logger.update(score=[x], total_steps=i)
运行会在当前目录生成一个logs文件,目录如下,其中csv里面记录了对应曲线mean_score,total_steps,std_score,max_score,min_score字段的数据。
在logs的路径下运行就可以进行可是化
rl_plotter --show
绘制同一组数据的平均曲线及上下的标准误差阴影区间。
rl_plotter --show --avg_group --shaded_std
绘制同一组数据的平均曲线及与Mean估计误差相对应的阴影区域。
rl_plotter --show --avg_group --shaded_err
或同时绘制两种区域
rl_plotter --show --avg_group --shaded_std --shaded_err
下图左中右为参数--shaded_std 、 --shaded_err 、 --shaded_std --shaded_err 绘制的图。对应曲线名称我们可以直接修改文件夹中组的名称,并添加上序号。对于图中曲线距离边框有一段距离,可以添加参数--xlim 10000,10000为x轴的步长参数,这样会调用plt.xlim((0, args.xlim))取出边框。
可以使用rl_plotter --help 查看所有参数
--fig_length matplotlib figure length (default: 8)
--fig_width matplotlib figure width (default: 6)
--style matplotlib figure style (default: seaborn)
--title matplotlib figure title (default: None)
--xlabel matplotlib figure xlabel
--xkey x-axis key in csv file (default: l)
--ykey y-axis key in csv file (support multi) (default: r)
--yduel duel y axis (use if has two ykeys)
--ylabel matplotlib figure ylabel
--smooth smooth radius of y axis (default: 10)
--resample if not zero, size of the uniform grid in x direction
to resample onto. Resampling is performed via
symmetric EMA smoothing (see the docstring for
symmetric_ema). Default is zero (no resampling). Note
that if average_group is True, resampling is
necessary; in that case, default value is 512.
(default: 512)
--smooth_step when resampling (i.e. when resample > 0 or
average_group is True), use this EMA decay parameter
(in units of the new grid step). See docstrings for
decay_steps in symmetric_ema or one_sided_ema functions.
(default: 1.0)
--avg_group average the curves in the same group and plot the mean
--shaded_std shaded region corresponding to standard deviation of the group
--shaded_err shaded region corresponding to error in mean estimate of the group
--legend_loc location of legend
--legend_outside place the legend outside of the figure
--borderpad borderpad of legend (default: 0.5)
--labelspacing labelspacing of legend (default: 0.5)
--no_legend_group_num don't show num of group in legend
--time enable this will activate parameters about time
--time_unit parameters about time, x axis time unit (default: h)
--time_interval parameters about time, x axis time interval (default: 1)
--xformat x-axis format
--xlim x-axis limitation (default: None)
--log_dir log dir (default: ./)
--filters filter of dirname
--filename csv filename
--show show figure
--save save figure
--dpi figure dpi (default: 400)
常用参数介绍
--fig_length
--fig_width
--title 图的顶部title
--xlabel x axis的label
--xkey x-axis key in csv file (default: l)
--ykey y-axis key in csv file (support multi) (default: r)
--yduel duel y axis (use if has two ykeys) 左右两侧y axis分别显示
--ylabel matplotlib figure ylabel
--smooth smooth radius of y axis (default: 10)
--avg_group average the curves in the same group and plot the mean
--shaded_std shaded region corresponding to standard deviation of the group
--legend_loc location of legend( 不同数字代表不同的位置)
--legend_outside 图例显示在图标外面
--labelspacing labelspacing of legend 图例间的距离(default: 0.5)
--no_legend_group_num don't show num of group in legend
--xformat x-axis format(end 、 log 、 sci)
--xlim x-axis limitation (default: None)
--log_dir log dir (default: ./)
--filters 过滤出要显示的曲线
--filename csv filename(指定显示的csv文件名)
--show show figure
--save save figure
--dpi figure dpi (default: 400)
#详细参数参考项目的plotter.py
该项目还支持是使用spiningup的kernel绘图
使用 rl_plotter_spinup --help查看详细参数,用法与rl_plotter类似