全球信息:【RL】算法简介与实现 Value-Based-Learning算法
一 Value-Based
Q-Learning
(资料图片)
Q-Learning是RL算法中Value-Based的算法,Q即为Q(s,a)就是在某一时刻的s状态下(s∈S),采取 动作a (a∈A)能够获得收益的期望,环境会根据agent的动作反馈相应的回报reward。所以算法的主要思想就是将State与Action构建成一张Q-table来存储Q值,然后根据Q值来选取能够获得最大的收益的动作。 下面是Q-Learning的TensorFlow实现
import numpy as npimport pandas as pdclass QLearning: def __init__(self, actions, learning_rate=0.01, reward_decay=0.9, e_greedy=0.9): """ QLearning :param actions: :param learning_rate: :param reward_decay: :param e_greedy: """ self.actions = actions self.lr = learning_rate self.gamma = reward_decay self.epsilon = e_greedy self.q_table = pd.DataFrame(columns=self.actions) def chooseAction(self, observation): """Choose action with state and observation""" self.checkStateExist(observation) if np.random.uniform()<SELF.EPSILON: :]="" opt_actions="self.q_table.loc[observation," #="" ="="np.max(opt_actions)].index)"" return="" action="opt_actions.argmax()" updateparams(self,="" state,="" action,="" reward,="" self.checkstateexist(state_)="" q_pre="self.q_table.loc[state," state_="" !="terminal" self.gamma="" self.q_table.loc[state_,="" :].max()="" q_target="reward" self.q_table.loc[state,="" action]="" +="" *="" (q_target="" -="" q_pre)="" def="" checkstateexist(self,="" if="" state="" not="" in="" self.q_table="self.q_table.append(" pd.series([0]*len(self.actions),="" index="self.q_table.columns," name="state)" )
DQN
当状态动作很多时,Q-Learning使用Table存储Value的方式不再实用(甚至不可行)。
如何不使用Table而得到每个状态下采取各个动作的Value呢?DQN用神经网络将State映射到Value。 DQN是在Q-Learning的主框架上做了扩展,包括:
记忆库(用于重复学习,随机抽取的经历也打乱的状态之间的相关性,使神经网络的更新更有效率)MLP计算Q值暂时冻结Q_target参数(切断相关性),target网络用来计算Q现实
下面是DQN的TensorFlow实现
import tensorflow as tfimport numpy as npclass DeepQNet: def __init__(self, n_actions, n_features, learning_rate=0.01, reward_decay=0.9, e_greedy=0.9, update_target_iter=300, memory_size=500, batch_size=32, e_greedy_increment=None, output_graph=False, ): """ DQN :param n_actions: :param n_features: :param learning_rate: :param reward_decay: :param e_greedy: :param update_target_iter: :param memory_size: :param batch_size: :param e_greedy_increment: :param output_graph: """ self.n_actions = n_actions self.n_actions = n_actions self.n_features = n_features self.lr = learning_rate self.gamma = reward_decay self.epsilon_max = e_greedy self.update_target_iter = update_target_iter self.memory_size = memory_size self.batch_size = batch_size self.epsilon_increment = e_greedy_increment self.epsilon = 0 if e_greedy_increment is not None else self.epsilon_max # total learning step(Cooperate with update_target_iter in learn() to update the parameters of target net) self.learn_step_counter = 0 # memory: row = memory_size, col = observation + observation_ + action + reward self.memory = np.zeros((self.memory_size, self.n_features*2+2)) self._buildNet() self.sess = tf.Session() if output_graph: tf.summary.FileWriter("logs/", self.sess.graph) self.sess.run(tf.global_variables_initializer()) self.cost = [] def _buildNet(self): """"Build evaluate network and target network""" # build evaluate net self.state = tf.placeholder(tf.float32, [None, self.n_features], name="state") self.q_target = tf.placeholder(tf.float32, [None, self.n_actions], name="Q_target") with tf.variable_scope("evaluate_net"): c_names, n_l1 = ["evaluate_net_params", tf.GraphKeys.GLOBAL_VARIABLES], 10 w_initializer, b_initializer = tf.random_normal_initializer(0, 0.3), tf.constant_initializer(0.1) with tf.variable_scope("layer1"): w1 = tf.get_variable("w1", [self.n_features, n_l1], initializer=w_initializer, collections=c_names) b1 = tf.get_variable("b1", [1, n_l1], initializer=b_initializer, collections=c_names) l1 = tf.nn.relu(tf.matmul(self.state, w1) + b1) with tf.variable_scope("layer2"): w2 = tf.get_variable("w2", [n_l1, self.n_actions], initializer=w_initializer, collections=c_names) b2 = tf.get_variable("b2", [1, self.n_actions], initializer=b_initializer, collections=c_names) self.q_evaluate = tf.nn.relu(tf.matmul(l1, w2) + b2) with tf.variable_scope("loss"): self.loss = tf.reduce_mean(tf.squared_difference(self.q_target, self.q_evaluate)) with tf.variable_scope("train"): self.opt = tf.train.RMSPropOptimizer(self.lr).minimize(self.loss) # build target net self.state_ = tf.placeholder(tf.float32, [None, self.n_features], name="state_") with tf.variable_scope("target_net"): c_names = ["target_net_params", tf.GraphKeys.GLOBAL_VARIABLES] with tf.variable_scope("layer1"): w1 = tf.get_variable("w1", [self.n_features, n_l1], initializer=w_initializer, collections=c_names) b1 = tf.get_variable("b1", [1, n_l1], initializer=b_initializer, collections=c_names) l1 = tf.nn.relu(tf.matmul(self.state_, w1) + b1) with tf.variable_scope("layer2"): w2 = tf.get_variable("w2", [n_l1, self.n_actions], initializer=w_initializer, collections=c_names) b2 = tf.get_variable("b2", [1, self.n_actions], initializer=b_initializer, collections=c_names) self.q_next = tf.nn.relu(tf.matmul(l1, w2) + b2) def storeTransition(self, state, action, reward, state_): """Store the state, observation and reward experienced during the train process to enable batch training""" if not hasattr(self, "memory_counter"): self.memory_counter = 0 transition = np.hstack((state, [action, reward], state_)) index = self.memory_counter % self.memory_size self.memory[index, :] = transition self.memory_counter += 1 def chooseAction(self, observation): """Choose action with state and observation""" observation = observation[np.newaxis, :] if np.random.uniform() < self.epsilon: actions = self.sess.run(self.q_evaluate, feed_dict={self.state: observation}) action = np.argmax(actions) else: action = np.random.randint(0, self.n_actions) return action def updateTargetNet(self): """Update the target net with the latest evaluate net parameters""" evaluate_params = tf.get_collection("evaluate_net_params") target_params = tf.get_collection("target_net_params") self.sess.run([tf.assign(t, e) for t, e in zip(target_params, evaluate_params)]) def learn(self): # check to update target net if self.learn_step_counter % self.update_target_iter == 0: self.updateTargetNet() print("Update target net!") # Get batch training data from the memory if self.memory_counter > self.memory_size: sample_index = np.random.choice(self.memory_size, size=self.batch_size) else: sample_index = np.random.choice(self.memory_counter, size=self.batch_size) batch_memory = self.memory[sample_index, :] q_evaluate, q_next = self.sess.run([self.q_evaluate, self.q_next], feed_dict={self.state: batch_memory[:, 0:self.n_features], self.state_: batch_memory[:, -self.n_features:]}) q_target = q_evaluate.copy() batch_index = np.arange(self.batch_size, dtype=np.int32) eval_act_index = batch_memory[:, self.n_features].astype(int) reward = batch_memory[:, self.n_features + 1] # Related to memory format, here means [action, reward] q_target[batch_index, eval_act_index] = reward + self.gamma * np.max(q_next, axis=1) _, cost = self.sess.run([self.opt, self.loss], feed_dict={self.state: batch_memory[:, 0:self.n_features], self.q_target: q_target }) self.cost.append(cost) self.epsilon = self.epsilon + self.epsilon_increment if self.epsilon < self.epsilon_max else self.epsilon_max self.learn_step_counter += 1 def showCost(self): import matplotlib.pyplot as plt plt.plot(np.arange(len(self.cost)), self.cost) plt.ylabel("Cost") plt.xlabel("training steps") plt.show()
二 Policy-Based
直接输出动作,可以在连续区间内选择动作;而Value-Based要在连续区间中,对无数个动作计算价值选择行为是不可行的。
误差如何反向传递呢?没有误差,它的目的是选的动作在下次更有可能被选择,但怎么知道动作的好坏呢,用reward,reward小,动作在下次被选择的可能性增加的少。
Actor-Critic
Actor:Policy-Based,输入State,预测输出采取各种Action的概率。 Critic;Value-Based,输入State,预测输出当前State的Value,并与下一状态的next_stateValue求TD_error 在Actor-Critic中,Actor可以每一步都更新学习(而单纯的Policy-Based方法要在回合结束后才能更新)
但也带来了问题:由于两个网络在连续状态中更新参数,每次跟新前后的参数具有相关性,导致网络只能片面的看待问题,甚至学不到有效的参数,不能收敛。
TRPO
PPO
Deep Deterministic Policy Gradient(DDPG)
标签:
相关推荐:
最新新闻:
- d盘打不开怎么办? b盘打不开解决方法
- 电磁调速电机是什么原理?电磁调速电机常见故障分析
- 如何理解KMT字符串匹配算法?如何计算出KMT数组?
- 天天热门:NCBI-BLAST在线使用教程详细攻略(图解)
- 全球信息:【RL】算法简介与实现 Value-Based-Learning算法
- 电阻的单位是什么?SMT贴片电阻510R与510K的区别在哪?
- 环球看热讯:【企业版】Mule3的新增特点-云连接
- 天天观焦点:对数的计算公式是什么?对数的计算公式性质及推导过程
- 当前视点!戴尔XPS15/17笔记本2023款发布:搭载13代酷睿+40系显卡
- 对标比亚迪唐,领克大5座SUV领克08来了,或搭载魅族Flyme Auto车机
- 世界微动态丨《爆衣战士 零》Steam页面上线 支持简繁体中文
- 打赢了!科比坠机照片泄露案其遗孀获赔2885万美元
- 报道:iPhone16Pro有望采用屏下面容 2026年用上屏下摄像头
- 微美全息开发基于图像处理的动态手势交互系统:全球热点评
- 环球观速讯丨努比亚发布裸眼3D平板电脑
- 努比亚Z50 Ultra影像参数公布:35mm+85mm最佳拍档_天天聚看点
- 新华三集团亮相CHINC,以数字技术助力智慧医院高质量发展-百事通
- 新华三:以融合网络技术支撑智慧医院全场景业务数字变革 今热点
- 全球规模最大锂资源采选冶一体化项目在新疆和田启动_今日观点
- 《守望先锋2》匹配机制糟糕 开发者回应
- 停售6年的PS3再次获得固件更新 提高了系统性能_天天热门
- SOC射击新游《The Front》登陆Steam 预告片首曝
- 《最终幻想16》将不会有多个难度选项 新游戏+包含新战斗-每日精选
- Windows 11发布更新 iPhone也能连接PC了_天天日报
- 热点聚焦:融合创新,领军2023!鸿合科技联手合作伙伴加速推进幼教数字化
- 首发2399元!vivo S16新配色“春日悠蓝”今天正式预售_天天热文
- 世界快看:ChatGPT赋能?微软或将人工智能引入Windows 11
- 天天热资讯!VMware在MWC 2023发布创新成果,帮助扩展电信运营商和企业的5G能力
- 全球快资讯:iPhone SE 4今年发布:6.1英寸全面屏 京东方提供
- 国风城镇叠叠乐 《重檐》Steam页面上线
- 全球消息!新疆队宣布退出CBA联赛捐献全部资产 周琦做出回应
- 房东纠纷 英国国家电子游戏博物馆团队被锁在门外|新要闻
- 新动态:《最终幻想16》试玩版将于发售前两周推出 支持存档继承
- 【新要闻】北欧游戏展被员工指控 调查结果:或违反《反歧视法》
- SteamDeck二月热门游戏 《霍格沃兹之遗》登顶_每日快讯
- crampfish_世界看点
- 国产动作游戏《斩妖行2》开发中 沙漠截图欣赏-快消息
- 环球新动态:三星980 PRO固态硬盘大促:1TB 649元
- 天天快资讯丨印度把苹果坑了:代工还是富士康靠谱
- 环球微资讯!库存压力来了:显卡销量暴跌43%!
- 苹果重启iPhone SE 4:京东方屏幕!|环球速看
- 环球快看点丨捡漏!4K IPS显示器659元
- PLC触摸屏一体机是什么?触摸屏控制PLC的原理有哪些?
- 设备管理器打不开如何解决?设备管理器打不开解决方法
- u盘读写速度是多少?查看u盘读写速度的方法
- PS阈值主要用来做什么?PS阈值抠图教程
- 怎么将esd格式转换成iso格式?esd文件转换iso的方法
- 路由器的核心功能是什么?推荐几款核心路由器及相应报价
- 路由器管理员密码是什么?路由器的管理员密码详细介绍
- 骁龙636处理器性能怎么样?高通骁龙636处理器性能详解
- 汉翔大将军手写板驱动怎么用?汉翔大将军手写板驱动安装说明常见问题解答
- Word目录怎样做?Word目录怎么自动生成?Word目录操作方法介绍
- 弹出winlogon.exe应用程序错误怎么办?winlogon.exe应用程序错误原因分析及解决方法
- SmartPrinter支持哪些格式?SmartPrinter文档转换方法
- Usb无法识别的原因是什么?电脑usb无法识别的处理步骤
- 本地连接不见了怎么办?本地连接不见了原因分析及解决方案介绍
- 940MX显卡怎么样?GeForce 940MX详细测评
- 拳头账号怎么注册?拳头账号密码忘了怎么找回?
- tcpip.sys文件造成电脑蓝屏怎么解决?tcpip.sys文件造成电脑蓝屏修复方法
- 全球即时:小米无线键鼠套装2开售:首发价89 元
- 12G+512G旗舰机超值价2149元!
- 苹果MR头显隔空打字:iPhone要酸了|全球观天下
- iPhone 15 Pro Max曝光:更厚更重了
- 2023年最完美全面屏!努比亚Z50 Ultra官宣3月7日发布
- 香奈儿回应2万包包15分钟断了:可以就近选择精品店进行售后评估 环球热讯
- Xbox老板:《使命召唤》不会学《霍格沃茨之遗》搞独占内容-环球要闻
- 《最终幻想16》的故事有浪漫色彩 兄弟家庭国家之爱|新要闻
- 沧海电视剧李山魁找组织是第几集_电视剧内容介绍 环球通讯
- 环球热消息:梅西FIFA最佳连续三年第一票投给内马尔:姆巴佩顺位第二
- 《杀手》开发商开发新IP:在线幻想RPG游戏
- 今亮点!2023款欧拉好猫电车上市,售价12.98-16.58万元
- 吉田直树表示《最终幻想16》不会有多种难度选项-每日热讯
- 致敬魂斗罗《地狱之魂》即将更新双人合作玩法|世界视讯
- 创作者们的利器!耕升RTX 4070 Ti星极幻姬OC带你领略AI绘画的世界|天天头条
- 直径安-225 微软模拟飞行推出超大货机:天天微资讯