Tensorflow学习笔记4:分布式Tensorflow

简介

Tensorflow API提供了Cluster、Server以及Supervisor来支持模型的分布式训练。

关于Tensorflow的分布式训练介绍可以参考Distributed Tensorflow。简单的概括说明如下:

  • Tensorflow分布式Cluster由多个Task组成,每个Task对应一个tf.train.Server实例,作为Cluster的一个单独节点;
  • 多个相同作用的Task可以被划分为一个job,例如ps job作为参数服务器只保存Tensorflow model的参数,而worker job则作为计算节点只执行计算密集型的Graph计算。
  • Cluster中的Task会相对进行通信,以便进行状态同步、参数更新等操作。

Tensorflow分布式集群的所有节点执行的代码是相同的。分布式任务代码具有固定的模式:

# 第1步:命令行参数解析,获取集群的信息ps_hosts和worker_hosts,以及当前节点的角色信息job_name和task_index# 第2步:创建当前task结点的Server
cluster = tf.train.ClusterSpec({"ps": ps_hosts, "worker": worker_hosts})
server = tf.train.Server(cluster, job_name=FLAGS.job_name, task_index=FLAGS.task_index)# 第3步:如果当前节点是ps,则调用server.join()无休止等待;如果是worker,则执行第4步。
if FLAGS.job_name == "ps":server.join()# 第4步:则构建要训练的模型
# build tensorflow graph model# 第5步:创建tf.train.Supervisor来管理模型的训练过程
# Create a "supervisor", which oversees the training process.
sv = tf.train.Supervisor(is_chief=(FLAGS.task_index == 0), logdir="/tmp/train_logs")
# The supervisor takes care of session initialization and restoring from a checkpoint.
sess = sv.prepare_or_wait_for_session(server.target)
# Loop until the supervisor shuts down
while not sv.should_stop()# train model

 

Tensorflow分布式训练代码框架

根据上面说到的Tensorflow分布式训练代码固定模式,如果要编写一个分布式的Tensorlfow代码,其框架如下所示。

import tensorflow as tf# Flags for defining the tf.train.ClusterSpec
tf.app.flags.DEFINE_string("ps_hosts", "","Comma-separated list of hostname:port pairs")
tf.app.flags.DEFINE_string("worker_hosts", "","Comma-separated list of hostname:port pairs")# Flags for defining the tf.train.Server
tf.app.flags.DEFINE_string("job_name", "", "One of 'ps', 'worker'")
tf.app.flags.DEFINE_integer("task_index", 0, "Index of task within the job")FLAGS = tf.app.flags.FLAGSdef main(_):ps_hosts = FLAGS.ps_hosts.split(",")worker_hosts = FLAGS.worker_hosts(",")# Create a cluster from the parameter server and worker hosts.cluster = tf.train.ClusterSpec({"ps": ps_hosts, "worker": worker_hosts})# Create and start a server for the local task.server = tf.train.Server(cluster,job_name=FLAGS.job_name,task_index=FLAGS.task_index)if FLAGS.job_name == "ps":server.join()elif FLAGS.job_name == "worker":# Assigns ops to the local worker by default.
    with tf.device(tf.train.replica_device_setter(worker_device="/job:worker/task:%d" % FLAGS.task_index,cluster=cluster)):# Build model...loss = ...global_step = tf.Variable(0)train_op = tf.train.AdagradOptimizer(0.01).minimize(loss, global_step=global_step)saver = tf.train.Saver()summary_op = tf.merge_all_summaries()init_op = tf.initialize_all_variables()# Create a "supervisor", which oversees the training process.sv = tf.train.Supervisor(is_chief=(FLAGS.task_index == 0),logdir="/tmp/train_logs",init_op=init_op,summary_op=summary_op,saver=saver,global_step=global_step,save_model_secs=600)# The supervisor takes care of session initialization and restoring from# a checkpoint.sess = sv.prepare_or_wait_for_session(server.target)# Start queue runners for the input pipelines (if any).
    sv.start_queue_runners(sess)# Loop until the supervisor shuts down (or 1000000 steps have completed).step = 0while not sv.should_stop() and step < 1000000:# Run a training step asynchronously.# See `tf.train.SyncReplicasOptimizer` for additional details on how to# perform *synchronous* training._, step = sess.run([train_op, global_step])if __name__ == "__main__":tf.app.run()

对于所有Tensorflow分布式代码,可变的只有两点:

  1. 构建tensorflow graph模型代码;
  2. 每一步执行训练的代码

分布式MNIST任务

我们通过修改tensorflow/tensorflow提供的mnist_softmax.py来构造分布式的MNIST样例来进行验证。修改后的代码请参考mnist_dist.py。

我们同样通过tensorlfow的Docker image来启动一个容器来进行验证。

$ docker run -d -v /path/to/your/code:/tensorflow/mnist --name tensorflow tensorflow/tensorflow

启动tensorflow之后,启动4个Terminal,然后通过下面命令进入tensorflow容器,切换到/tensorflow/mnist目录下

$ docker exec -ti tensorflow /bin/bash
$ cd /tensorflow/mnist

然后在四个Terminal中分别执行下面一个命令来启动Tensorflow cluster的一个task节点,

# Start ps 0
python mnist_dist.py --ps_hosts=localhost:2221,localhost:2222 --worker_hosts=localhost:2223,localhost:2224 --job_name=ps --task_index=0# Start ps 1
python mnist_dist.py --ps_hosts=localhost:2221,localhost:2222 --worker_hosts=localhost:2223,localhost:2224 --job_name=ps --task_index=1# Start worker 0
python mnist_dist.py --ps_hosts=localhost:2221,localhost:2222 --worker_hosts=localhost:2223,localhost:2224 --job_name=worker --task_index=0# Start worker 1
python mnist_dist.py --ps_hosts=localhost:2221,localhost:2222 --worker_hosts=localhost:2223,localhost:2224 --job_name=worker --task_index=1

具体效果自己验证哈。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542330.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c语言指针访问 静态变量_使用C中的指针访问变量的值

c语言指针访问 静态变量As we know that a pointer is a special type of variable that is used to store the memory address of another variable. A normal variable contains the value of any type like int, char, float etc, while a pointer variable contains the me…

迭代器 java_Java设计模式8:迭代器模式

迭代器模式迭代器模式又叫做游标(Cursor)模式&#xff0c;其作用是提供一种方法访问一个容器元素中的各个对象&#xff0c;而又不暴露该对象的内部细节。迭代器模式结构迭代器模式由以下角色组成&#xff1a;1、迭代器角色负责定义访问和遍历元素的接口2、具体迭代器角色实现迭…

html二级下拉菜单模板,基于jQuery实现二级下拉菜单效果

本文通过代码实例详细介绍一下简单的二级下拉菜单是如何实现的&#xff0c;当然还有更为复杂的二级菜单&#xff0c;不过先学会如何制作简单的&#xff0c;分享给大家供大家参考&#xff0c;具体内容如下代码如下&#xff1a;下拉菜单nav a{text-decoration:none;}nav>ul>…

给定一个整数判断是否为素数_Ruby程序检查给定数字是否为素数

给定一个整数判断是否为素数检查素数 (Checking prime number) Before getting into writing the code, let us understand what exactly the prime numbers are? So that we could easily design its logic and implement it in the code. Prime numbers are those numbers w…

python 正则findall右斜杠_python中正则表达式的使用

本文将介绍几个最常用的正则符号&#xff0c;以及正则表达式的应用场景。如果说【数学表达式】刻画的是数字的内在规律&#xff0c;那么【正则表达式】则是用来刻画和描述字符串内在规律的表达式。记得刚接触python时学习过slice&#xff0c;replace&#xff0c;split等方法&am…

JavaScript | 用户定义函数的一些示例

1) Design a function, print message and assign the function to a variable and print it like a function 1)设计一个功能&#xff0c;打印消息并将该功能分配给变量&#xff0c;然后像打印功能一样打印 <html lang"en"><head><script>functi…

网易 html5,别再想不开做H5了

写这篇文章的时候网易哒哒《饲养手册》H5刷屏了&#xff0c;但我们依旧不建议品牌做H5。H5作为大众传播工具的时代&#xff0c;已经过去了。尽管去年有很多H5曾经刷屏过&#xff0c;但在当时我们就一直跟朋友说&#xff0c;不要再尝试H5了&#xff0c;性价比根本算不过来&#…

python打开word后再关闭再打开出错_用Python写了个程序调用word,运行完后再手动打开word文档就变慢了,这是为啥?...

公司归档文件比较麻烦&#xff0c;于是用Python写了个程序自动归档&#xff0c;运行无错误。但是运行完后问题就来了&#xff0c;自己手动打开word文档时速度变得奇慢&#xff0c;打开一个文档需要1~2min,请各位同仁帮我看看。下为源代码#归档.pyimport osimport refrom win32c…

编程 mcq_MCQ | 8255 PPI(可编程外围接口)

编程 mcqQuestion 1: How many pins does the 8255 PPI IC contains? 问题1&#xff1a;8255 PPI IC包含多少个引脚&#xff1f; 24 24 20 20 32 32 40 40 Answer: d. 40 答案&#xff1a;d。 40 Question 2: In which mode do all the Ports of the 8255 PPI work as Input…

flex 修改生成html,CSS Flex –动画教程

如果一张图片胜过千言万语 —— 那么动画呢&#xff1f; Flex 无法通过文字或静态图像有效地完全解释。为了巩固你对flex的了解&#xff0c;我制作了这些动画演示。注意 overflow: hidden 行为类型是默认值&#xff0c;因为 flex-wrap 还未设置。为了获得更好的想法&#xff0c…

c#c#继承窗体_C#继承能力问题和解答 套装5

c#c#继承窗体1) Which keyword is used to call a superclass constructor from child class? supertopconstbase Answer & Explanation Correct answer: 4base In C#.NET, base keyword is used to call a base class constructor from a derived class. 1)使用哪个关键字…

python php 网站_python php网站

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里技术人对外发布原创技术内容的最大平台&…

陕西2021高考成绩在哪查询,2021陕西高考成绩查询入口

2021陕西高考成绩查询入口2021-05-13 19:38:37文/张敏有很多同学在关注2021年陕西高考成绩的查询方式&#xff0c;为了方便考生们查询成绩&#xff0c;小编整理了陕西高考成绩查询入口&#xff0c;希望对同学们有帮助。2021陕西高考成绩查询通道高考成绩查询过后应该做什么1、了…

Can’t Activate Reporting Services Service in SharePoint

访问sharepoint的reporing service 的报表的时候莫名其妙的报错&#xff1a; The requested service, http://amatltapp02:32843/1dacf49a2f7a4a6daa8db5768539893f/ReportingWebService.svc could not be activated. See the servers diagnostic trace logs for more informat…

scala python_Scala与Python | 哪种编程语言更好

scala pythonScala is a general-purpose programming language developed by Martin Odersky in 2004. Scala是Martin Odersky在2004年开发的通用编程语言。 Both Scala and Python are general purpose programming that is used in Data Science that supports Object Orie…

查找文件中每行第二个单词_日语单词中的长短音区别在哪里,日语长短音发音有什么规律...

日语单词记忆长短音规律一、如果单词的汉字在中文汉语拼音中是前鼻音&#xff0c;在日语读音中就会带拨音「ん」&#xff1b; 如果单词的汉字在中文汉语拼音中是后鼻音&#xff0c;在日语读音中就会带有长音。例&#xff1a;専门&#xff08;zhuan men&#xff09;&#xff0d;…

SQL Server 执行计划利用统计信息对数据行的预估原理二(为什么复合索引列顺序会影响到执行计划对数据行的预估)...

本文出处&#xff1a;http://www.cnblogs.com/wy123/p/6008477.html 关于统计信息对数据行数做预估&#xff0c;之前写过对非相关列&#xff08;单独或者单独的索引列&#xff09;进行预估时候的算法&#xff0c;参考这里。  今天来写一下统计信息对于复合索引在预估时候的计…

计算机三四级网络技术,全国计算机等级考试四级网络技术论述题真题3

1.(2003年)网络安全策略设计的重要内容之一是&#xff1a;确定当网络安全受到威胁时应采取的应急措施。当我们发现网络受到非法侵入与攻击时&#xff0c;所能采取的行动方案基本上有两种&#xff1a;保护方式与跟踪方式。请根据你对网络安全方面知识的了解&#xff0c;讨论以下…

哈密顿路径_检查图形是否为哈密顿量(哈密顿路径)

哈密顿路径Problem Statement: 问题陈述&#xff1a; Given a graph G. you have to find out that that graph is Hamiltonian or not. 给定图G。 您必须找出该图是否为哈密顿量 。 Example: 例&#xff1a; Input: 输入&#xff1a; Output: 1 输出1 Because here is a …

京东自动下单软件_黄牛软件自动下单秒杀商品 警方用科技手段打击

法制日报全媒体记者 张维定了10个闹钟,也抢不到一瓶茅台&#xff1b;等了很久的iPhone新手机,打开网页就秒没……或许并不是因为你的手速、网速慢,而是黄牛党在用软件和你抢商品。近日,在“净网2019”专项行动中,阿里安全协助江苏省南通市公安局成功打掉了一个制作销售黄牛软件…