Python小练习:线性衰减
Python 练习 线性
2023-09-27 14:25:20 时间
Python小练习:线性衰减
作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/
本文介绍一种最简单的衰减曲线:线性衰减。给定schedule = [start, end, start_value, end_value],先前一直保持在start_value水平,从start时刻开始衰减,直至到达end时刻结束,其值为end_value,之后就一直保持在end_value这一水平上不变。
1. get_scheduled_value_test.py
1 # -*- coding: utf-8 -*- 2 # Author:凯鲁嘎吉 Coral Gajic 3 # https://www.cnblogs.com/kailugaji/ 4 # Python小练习:线性衰减 5 import numpy as np 6 import matplotlib.pyplot as plt 7 plt.rc('font',family='Times New Roman') 8 # Scheduled Exploration Noise 9 # linear decay 10 def get_scheduled_value(current, schedule): 11 start, end, start_value, end_value = schedule 12 ratio = (current - start) / (end - start) # 当前步数在总步数的比例 13 # 总计100步,当前current步 14 ratio = max(0, min(1, ratio)) 15 value = (ratio * (end_value - start_value)) + start_value 16 return value 17 18 start = 10 # 从这时开始衰减 19 end = 100 # the decay horizon 20 start_value = 1 # 从1衰减到0.1 21 end_value = 0.1 22 schedule = [start, end, start_value, end_value] 23 exploration_noise = [] 24 for i in range(int(end - start)+1): 25 value = get_scheduled_value(start + i, schedule) 26 exploration_noise.append(value) 27 28 # --------------------画图------------------------ 29 # 手动设置横纵坐标范围 30 plt.xlim([0, end*1.3]) 31 plt.ylim([0, start_value + 0.1]) 32 my_time = np.arange(start, end+1) 33 exploration_noise = np.array(exploration_noise) 34 plt.plot([0, start], [start_value, start_value], color = 'red', ls = '-') 35 plt.plot(my_time, exploration_noise, color = 'red', ls = '-') 36 plt.plot([end, end*1.3], [end_value, end_value], color = 'red', ls = '-') 37 # 画3条不起眼的虚线 38 plt.plot([0, end*1.3], [exploration_noise[-1], exploration_noise[-1]], color = 'gray', ls = '--', alpha = 0.3) 39 plt.text(end - end/3, exploration_noise[-1] + 0.03, "y = %.2f" %exploration_noise[-1], fontdict={'size': '12', 'color': 'gray'}) 40 plt.plot([start, start], [0, start_value + 0.1], color = 'gray', ls = '--', alpha = 0.3) 41 plt.text(start + 0.5, start_value - 0.8, "x = %d" %start, fontdict={'size': '12', 'color': 'gray'}) 42 plt.plot([end, end], [0, start_value + 0.1], color = 'gray', ls = '--', alpha = 0.3) 43 plt.text(end + 0.5, start_value - 0.8, "x = %d" %end, fontdict={'size': '12', 'color': 'gray'}) 44 # 横纵坐标轴 45 plt.xlabel('Timestep') 46 plt.ylabel('Linear Decay') 47 plt.tight_layout() 48 plt.savefig('Linear Decay.png', bbox_inches='tight', dpi=500) 49 plt.show()
2. 结果
3. 参考文献
[1] Yarats D, Fergus R, Lazaric A, et al. Mastering visual continuous control: Improved data-augmented reinforcement learning[J]. arXiv preprint arXiv:2107.09645, 2021.
相关文章
- Python编程 基础练习(二)
- [ Python - 10 ] 练习:批量管理主机工具
- Java、PHP、Python、JS 等开发者都如何绘制统计图
- windows 环境下python 安装 pypcap 并用pyinstaller打包到exe,解决DLL 加载失败。
- python读取文件内的IP信息 练习
- Python+Selenium —— Web自动化测试:元素定位
- python websocket-client connection
- Python 实现MD5加密
- python 删除非空文件夹
- 通过python xlsxwriter模块生成EXCEL柱状图、饼图
- Python小练习:绘制散点图并添加基线
- 【Python】分支结构:单分支结构(if 语句)
- Python 练习:三级菜单选择城市
- python练习:烤鸡翅
- Python基础三(选择,循环)
- python小练习12--排序和查找的工具类
- python小练习8-- 2048游戏
- python 小练习1
- 第5章函数进阶 第5.1节 Python函数的位置参数、关键字参数精讲
- 第4.7节 Python特色的序列解包、链式赋值、链式比较