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.pswp.cn/news/542330.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542330.shtml
英文地址,請注明出處:http://en.pswp.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”專項行動中,阿里安全協助江蘇省南通市公安局成功打掉了一個制作銷售黃牛軟件…