在本例中(代码在文末),我们将使用OpenAI Gym训练一个非常简单的神经网络来打乒乓球。这个应用程序改编自Andrej Karpathy的代码,只做了很少的修改(请参阅附带的博客文章)。

首先安装依赖包gym

pip install gym[atari]

运行代码:

python ray/examples/rl_pong/driver.py --batch-size=10

如果运行在集群上,在后边加上--redis-address=<redis-address> 标志,其中redis-address为集群IP地址,如下:

python ray/examples/rl_pong/driver.py --batch-size=10  --redis-address=<redis-address>

目前,在拥有64个物理内核的大型计算机上,使用批处理大小为1的更新计算大约需要1秒,而使用批处理大小为10的更新计算大约需要2.5秒。批处理大小为60的代码大约需要3秒。在一个有11个节点(每个节点都有18个物理内核)的集群中,批量为300个节点大约需要10秒。如果您看到的数字与这些数字相差很大,请查看本文底部的故障排除部分,并考虑提交一个问题。
注意 ,这些时间取决于推出所需的时间,而这又取决于政策的执行情况。例如,一个非常糟糕的政策很快就会失败。随着政策的发展,我们应该预计这些数字还会增加。

分布式框架

在Andrej代码的核心,神经网络被用来定义打乒乓球的“策略”(也就是说,一个函数选择给的定状态的动作)。在循环中,网络反复播放乒乓球游戏,并记录每个游戏的梯度。每十场比赛,梯度组合在一起,用来更新网络。

这个例子很容易并行化,因为网络可以并行运行10个游戏,而且游戏之间不需要共享任何信息。

为Pong环境定义了一个actor,其中包括一个执行滚动和计算渐变更新的方法。下面是参与者的伪代码。

@ray.remote
class PongEnv(object):def __init__(self):# 告诉numpy只使用一个核心。 如果我们不这样做,每个参与者可能# 会尝试使用所有核心,由此产生的争用可能导致串行版本没有加速。#  请注意,如果numpy正在使用OpenBLAS,那么您需要设置# OPENBLAS_NUM_THREADS = 1,并且您可能需要从命令行执行此操作#(因此它在导入numpy之前发生)。os.environ["MKL_NUM_THREADS"] = "1"self.env = gym.make("Pong-v0")def compute_gradient(self, model):# Reset the game.observation = self.env.reset()while not done:# Choose an action using policy_forward.# Take the action and observe the new state of the world.# Compute a gradient using policy_backward. Return the gradient and reward.return [gradient, reward_sum]

然后,我们创建一些参与者,以便能够并行地执行滚动。

actors = [PongEnv() for _ in range(batch_size)]

在for循环中调用这个远程函数,启动多个任务来执行滚动并并行计算梯度

model_id = ray.put(model)
actions = []
# Launch tasks to compute gradients from multiple rollouts in parallel.
for i in range(batch_size):action_id = actors[i].compute_gradient.remote(model_id)actions.append(action_id)

故障排除

如果您没有看到Ray的任何加速(假设您使用的是多核机器),那么问题可能是numpy试图使用多线程。当许多进程都试图使用多个线程时,结果往往是没有加速。运行此示例时,请尝试打开top,查看一些python进程是否使用了超过100%的CPU。如果是,那么这可能就是问题所在。

示例尝试在参与者中设置MKL_NUM_THREADS=1。但是,只有在您的机器上的numpy实际使用MKL时才可以这样做。如果使用OpenBLAS,那么需要将OPENBLAS_NUM_THREADS设置为1。实际上,您可能必须在运行脚本之前执行此操作(可能需要在导入numpy之前执行)。
此例代码:

# This code is copied and adapted from Andrej Karpathy's code for learning to
# play Pong https://gist.github.com/karpathy/a4166c7fe253700972fcbc77e4ea32c5.from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport argparse
import numpy as np
import os
import ray
import timeimport gym# Define some hyperparameters.# The number of hidden layer neurons.
H = 200
learning_rate = 1e-4
# Discount factor for reward.
gamma = 0.99
# The decay factor for RMSProp leaky sum of grad^2.
decay_rate = 0.99# The input dimensionality: 80x80 grid.
D = 80 * 80def sigmoid(x):# Sigmoid "squashing" function to interval [0, 1].return 1.0 / (1.0 + np.exp(-x))def preprocess(img):"""Preprocess 210x160x3 uint8 frame into 6400 (80x80) 1D float vector."""# Crop the image.img = img[35:195]# Downsample by factor of 2.img = img[::2, ::2, 0]# Erase background (background type 1).img[img == 144] = 0# Erase background (background type 2).img[img == 109] = 0# Set everything else (paddles, ball) to 1.img[img != 0] = 1return img.astype(np.float).ravel()def discount_rewards(r):"""take 1D float array of rewards and compute discounted reward"""discounted_r = np.zeros_like(r)running_add = 0for t in reversed(range(0, r.size)):# Reset the sum, since this was a game boundary (pong specific!).if r[t] != 0:running_add = 0running_add = running_add * gamma + r[t]discounted_r[t] = running_addreturn discounted_rdef policy_forward(x, model):h = np.dot(model["W1"], x)h[h < 0] = 0  # ReLU nonlinearity.logp = np.dot(model["W2"], h)p = sigmoid(logp)# Return probability of taking action 2, and hidden state.return p, hdef policy_backward(eph, epx, epdlogp, model):"""backward pass. (eph is array of intermediate hidden states)"""dW2 = np.dot(eph.T, epdlogp).ravel()dh = np.outer(epdlogp, model["W2"])# Backprop relu.dh[eph <= 0] = 0dW1 = np.dot(dh.T, epx)return {"W1": dW1, "W2": dW2}@ray.remote
class PongEnv(object):def __init__(self):# Tell numpy to only use one core. If we don't do this, each actor may# try to use all of the cores and the resulting contention may result# in no speedup over the serial version. Note that if numpy is using# OpenBLAS, then you need to set OPENBLAS_NUM_THREADS=1, and you# probably need to do it from the command line (so it happens before# numpy is imported).os.environ["MKL_NUM_THREADS"] = "1"self.env = gym.make("Pong-v0")def compute_gradient(self, model):# Reset the game.observation = self.env.reset()# Note that prev_x is used in computing the difference frame.prev_x = Nonexs, hs, dlogps, drs = [], [], [], []reward_sum = 0done = Falsewhile not done:cur_x = preprocess(observation)x = cur_x - prev_x if prev_x is not None else np.zeros(D)prev_x = cur_xaprob, h = policy_forward(x, model)# Sample an action.action = 2 if np.random.uniform() < aprob else 3# The observation.xs.append(x)# The hidden state.hs.append(h)y = 1 if action == 2 else 0  # A "fake label".# The gradient that encourages the action that was taken to be# taken (see http://cs231n.github.io/neural-networks-2/#losses if# confused).dlogps.append(y - aprob)observation, reward, done, info = self.env.step(action)reward_sum += reward# Record reward (has to be done after we call step() to get reward# for previous action).drs.append(reward)epx = np.vstack(xs)eph = np.vstack(hs)epdlogp = np.vstack(dlogps)epr = np.vstack(drs)# Reset the array memory.xs, hs, dlogps, drs = [], [], [], []# Compute the discounted reward backward through time.discounted_epr = discount_rewards(epr)# Standardize the rewards to be unit normal (helps control the gradient# estimator variance).discounted_epr -= np.mean(discounted_epr)discounted_epr /= np.std(discounted_epr)# Modulate the gradient with advantage (the policy gradient magic# happens right here).epdlogp *= discounted_eprreturn policy_backward(eph, epx, epdlogp, model), reward_sumif __name__ == "__main__":parser = argparse.ArgumentParser(description="Train an RL agent on Pong.")parser.add_argument("--batch-size",default=10,type=int,help="The number of rollouts to do per batch.")parser.add_argument("--redis-address",default=None,type=str,help="The Redis address of the cluster.")parser.add_argument("--iterations",default=-1,type=int,help="The number of model updates to perform. By ""default, training will not terminate.")args = parser.parse_args()batch_size = args.batch_sizeray.init(redis_address=args.redis_address)# Run the reinforcement learning.running_reward = Nonebatch_num = 1model = {}# "Xavier" initialization.model["W1"] = np.random.randn(H, D) / np.sqrt(D)model["W2"] = np.random.randn(H) / np.sqrt(H)# Update buffers that add up gradients over a batch.grad_buffer = {k: np.zeros_like(v) for k, v in model.items()}# Update the rmsprop memory.rmsprop_cache = {k: np.zeros_like(v) for k, v in model.items()}actors = [PongEnv.remote() for _ in range(batch_size)]iteration = 0while iteration != args.iterations:iteration += 1model_id = ray.put(model)actions = []# Launch tasks to compute gradients from multiple rollouts in parallel.start_time = time.time()for i in range(batch_size):action_id = actors[i].compute_gradient.remote(model_id)actions.append(action_id)for i in range(batch_size):action_id, actions = ray.wait(actions)grad, reward_sum = ray.get(action_id[0])# Accumulate the gradient over batch.for k in model:grad_buffer[k] += grad[k]running_reward = (reward_sum if running_reward is None elserunning_reward * 0.99 + reward_sum * 0.01)end_time = time.time()print("Batch {} computed {} rollouts in {} seconds, ""running mean is {}".format(batch_num, batch_size,end_time - start_time,running_reward))for k, v in model.items():g = grad_buffer[k]rmsprop_cache[k] = (decay_rate * rmsprop_cache[k] + (1 - decay_rate) * g**2)model[k] += learning_rate * g / (np.sqrt(rmsprop_cache[k]) + 1e-5)# Reset the batch gradient buffer.grad_buffer[k] = np.zeros_like(v)batch_num += 1

Ray实例-乒乓球训练学习相关推荐

  1. keras构建前馈神经网络(feedforward neural network)进行多分类模型训练学习

    keras构建前馈神经网络(feedforward neural network)进行多分类模型训练学习 前馈神经网络(feedforward neural network)是一种最简单的神经网络,各 ...

  2. 乒乓球训练机_比教练更牛的全新乒乓球机器人,超拟人黑科技,引领未来体育浪潮...

    乒乓球虽然被誉为我国"国球",但一直以来在民间的普及度并不高,主要有以下四大痛点: ①上手难度大,绝大多数球友还处于初级阶段,很难体验到乒乓球旋转和高质量对抗带来的快感. ②很难找 ...

  3. SHU汇编程序设计常见考点、易错点总结与综合实例、汇编学习资源

    文章目录 SHU汇编程序设计常见考点.易错点总结与综合实例.汇编学习资源 一.常见考点 1.寻址方式 2.移位 3.乘除运算 4.加减操作 5.取址操作 6.出入栈 7.中断 8.标志位 9.常见的字 ...

  4. c语言编程实例解析精粹,C语言实例解析精粹学习笔记——35(报数游戏)

    实例35: 设由n个人站成一圈,分别被编号1,2,3,4,--,n.第一个人从1开始报数,每报数位m的人被从圈中推测,其后的人再次从1开始报数,重复上述过程,直至所有人都从圈中退出. 实例解析: 用链 ...

  5. 《After Effects CC中文版超级学习手册》——2.6 基础操作实例:欢迎学习AE CC打板动画...

    本节书摘来自异步社区<After Effects CC中文版超级学习手册>一书中的第2章,第2.6节,作者 程明才,更多章节内容可以访问云栖社区"异步社区"公众号查看. ...

  6. 一种智能乒乓球训练系统

    乒乓球是一种对抗类体育运动,需要比赛双方配合才能进行有效的训练和比赛,但是训练中常常难于寻找合适的对手,为此设计开发具有单人训练功能的乒乓球器材非常重要.乒乓球计数和计时功能是乒乓球训练中的核心指标, ...

  7. 训练选好拍 少走弯路进步快 SJMD智能乒乓球训练拍

    俗话说得好:好马配好鞍,好船配好帆.要想乒乓球打得好,坚持训练不可少,在训练球技的过程中,很多球友都是靠主观意识及对方接球效果来判断,缺乏客观事实数据的分析,常常因为训练效率较低而烦恼.跟高手过招吧, ...

  8. halcon第十一讲:基于训练学习的木板纹理识别

    现有以下五组木板图像,分别为A,B,C,D,E,通过训练学习识别出它们:

  9. 2.1 为什么要进行实例探究-深度学习第四课《卷积神经网络》-Stanford吴恩达教授

    ←上一篇 ↓↑ 下一篇→ 1.12 总结 回到目录 2.2 经典网络 为什么要进行实例探究 (Why look at case studies?) 这周我们首先来看看一些卷积神经网络的实例分析,为什么 ...

最新文章

  1. 解决win7不能上网的问题
  2. Python培训分享:python爬虫可以用来做什么?
  3. 构建之法现代软件工程(第五次)
  4. Matlab学习笔记——数据文件定位
  5. 25 uname-用于显示系统信息
  6. python3菜鸟教程-总算理解python3中文入门教程
  7. 对模型方差和偏差的解释之一:过拟合
  8. vba 自动排序_学会这个Excel表格技巧之后,立刻实现自动排序,太牛了
  9. tableau暂时不支持m1芯片!期待未来!
  10. 【C语言】这是我能想到的最难的题了
  11. 实战:从Mysql数据库frm文件中,提取表结构创建SQL语句
  12. Python参考手册
  13. 利用公网Msf+MS17010跨网段攻击内网
  14. 批量生成Code128- C条码
  15. graylog+kafka+zookeeper(单机测试及源码),graylog组件部署,查找问题分析(一)
  16. 用手机计算机计算三次根号,手机自带计算器不行求推荐一个能开3次根号的 – 手机爱问...
  17. NLP自然语言处理学习
  18. ambari部署hadoop
  19. 21年1.9c#halcon机器视觉软件系统框架源码visi onpro
  20. 2019年高考数学立体几何解题技巧分析策略

热门文章

  1. 异步html的效果,异步加载非核心CSS_html/css_WEB-ITnose
  2. 学习检测:6818电子相册基础功能
  3. 慕课: 如何用offcie MIX制作视频教--慕课背景下的教学重构
  4. 微服务架构统一异常监控Sentry
  5. 恭王府内部景点最短路径
  6. Dubbox简介与入门实战
  7. 模仿手机QQ红点消除功能
  8. 国内操作系统项目销声匿迹
  9. 最好用的Java热部署工具,无需重启项目秒级编译class代码
  10. MFC用TextOut输出的文本怎样擦除