{"id":258,"date":"2018-07-20T11:34:30","date_gmt":"2018-07-20T11:34:30","guid":{"rendered":""},"modified":"2020-10-01T02:04:19","modified_gmt":"2020-09-30T18:04:19","slug":"%e6%90%ad%e5%bb%ba%e6%9c%80%e7%ae%80%e6%98%93%e7%9a%84tensorflow%e9%9b%86%e7%be%a4","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=258","title":{"rendered":"\u642d\u5efa\u6700\u7b80\u6613\u7684TensorFlow\u96c6\u7fa4"},"content":{"rendered":"<p><span style=\"font-size: 14px;\">1\u3001\u88c5\u597d\u4e09\u53f0\u865a\u62df\u673atf1, tf2, tf3<\/span><\/p>\n<p><span style=\"font-size: 14px;\">\uff08\u7565\uff09<\/span><\/p>\n<p><span style=\"font-size: 14px;\">2\u3001\u5728tf1\u4e0a<\/span><\/p>\n<p><span style=\"font-size: 14px;\">vim server.py<\/span><\/p>\n<p><span style=\"font-size: 14px;\">import tensorflow as tf<\/span><\/p>\n<p><span style=\"font-size: 14px;\">worker1 = &#8220;tf1:10000&#8221;<\/span><br \/>\n<span style=\"font-size: 14px;\">worker2 = &#8220;tf2:10000&#8221;<\/span><br \/>\n<span style=\"font-size: 14px;\">worker3 = &#8220;tf3:10000&#8221;<\/span><\/p>\n<p><span style=\"font-size: 14px;\">worker_hosts = [worker1, worker2, worker3]<\/span><br \/>\n<span style=\"font-size: 14px;\">cluster_spec = tf.train.ClusterSpec({ &#8220;worker&#8221;: worker_hosts})<\/span><br \/>\n<span style=\"font-size: 14px;\">server = tf.train.Server(cluster_spec, job_name=&#8221;worker&#8221;, task_index=0)<\/span><br \/>\n<span style=\"font-size: 14px;\">server.join()<\/span><\/p>\n<p><span style=\"font-size: 14px;\">3\u3001\u5728tf2\u4e0a<\/span><\/p>\n<p><span style=\"font-size: 14px;\">vim server.py<\/span><\/p>\n<p><span style=\"font-size: 14px;\">import tensorflow as tf<\/span><\/p>\n<p><span style=\"font-size: 14px;\">worker1 = &#8220;tf1:10000&#8221;<\/span><br \/>\n<span style=\"font-size: 14px;\">worker2 = &#8220;tf2:10000&#8221;<\/span><br \/>\n<span style=\"font-size: 14px;\">worker3 = &#8220;tf3:10000&#8221;<\/span><\/p>\n<p><span style=\"font-size: 14px;\">worker_hosts = [worker1, worker2, worker3]<\/span><br \/>\n<span style=\"font-size: 14px;\">cluster_spec = tf.train.ClusterSpec({ &#8220;worker&#8221;: worker_hosts})<\/span><br \/>\n<span style=\"font-size: 14px;\">server = tf.train.Server(cluster_spec, job_name=&#8221;worker&#8221;, task_index=1)<\/span><br \/>\n<span style=\"font-size: 14px;\">server.join()<\/span><\/p>\n<p><span style=\"font-size: 14px;\">4\u3001\u5728tf3\u4e0a<\/span><\/p>\n<p><span style=\"font-size: 14px;\">vim server.py<\/span><\/p>\n<p><span style=\"font-size: 14px;\">import tensorflow as tf<\/span><\/p>\n<p><span style=\"font-size: 14px;\">worker1 = &#8220;tf1:10000&#8221;<\/span><br \/>\n<span style=\"font-size: 14px;\">worker2 = &#8220;tf2:10000&#8221;<\/span><br \/>\n<span style=\"font-size: 14px;\">worker3 = &#8220;tf3:10000&#8221;<\/span><\/p>\n<p><span style=\"font-size: 14px;\">worker_hosts = [worker1, worker2, worker3]<\/span><br \/>\n<span style=\"font-size: 14px;\">cluster_spec = tf.train.ClusterSpec({ &#8220;worker&#8221;: worker_hosts})<\/span><br \/>\n<span style=\"font-size: 14px;\">server = tf.train.Server(cluster_spec, job_name=&#8221;worker&#8221;, task_index=2)<\/span><br \/>\n<span style=\"font-size: 14px;\">server.join()<\/span><\/p>\n<p><span style=\"font-size: 14px;\">5\u3001\u5728\u4efb\u610f\u4e00\u53f0tfx\u4e3b\u673a\u4e0a<\/span><\/p>\n<p><span style=\"font-size: 14px;\">vim client.py<\/span><\/p>\n<p><span style=\"font-size: 14px;\">import tensorflow as tf<\/span><br \/>\n<span style=\"font-size: 14px;\">import numpy as np<\/span><\/p>\n<p><span style=\"font-size: 14px;\">train_X = np.linspace(-1,1,1000000)<\/span><br \/>\n<span style=\"font-size: 14px;\">train_Y = 2*train_X + np.random.randn(*train_X.shape)*0.33+10<\/span><\/p>\n<p><span style=\"font-size: 14px;\">X = tf.placeholder(&#8220;float&#8221;)<\/span><br \/>\n<span style=\"font-size: 14px;\">Y = tf.placeholder(&#8220;float&#8221;)<\/span><\/p>\n<p><span style=\"font-size: 14px;\">w = tf.Variable(0.0, name=&#8221;weight&#8221;)<\/span><br \/>\n<span style=\"font-size: 14px;\">b = tf.Variable(0.0, name=&#8221;reminder&#8221;)<\/span><\/p>\n<p><span style=\"font-size: 14px;\">init_op = tf.initialize_all_variables()<\/span><br \/>\n<span style=\"font-size: 14px;\">cost_op = tf.square(Y &#8211; tf.mul(X,w) &#8211; b)<\/span><br \/>\n<span style=\"font-size: 14px;\">train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost_op)<\/span><\/p>\n<p><span style=\"font-size: 14px;\">with tf.Session(&#8220;grpc:\/\/tf1:10000&#8221;) as sess:<\/span><br \/>\n<span style=\"font-size: 14px;\">\u00a0 with tf.device(&#8220;\/job:worker\/task:0&#8221;):<\/span><br \/>\n<span style=\"font-size: 14px;\">\u00a0 \u00a0 sess.run(init_op)<\/span><\/p>\n<p><span style=\"font-size: 14px;\">\u00a0 \u00a0 for i in range(10):<\/span><br \/>\n<span style=\"font-size: 14px;\">\u00a0 \u00a0 \u00a0 for (x, y) in zip(train_X, train_Y):<\/span><br \/>\n<span style=\"font-size: 14px;\">\u00a0 \u00a0 \u00a0 \u00a0 sess.run(train_op, feed_dict={X:x, Y:y})<\/span><\/p>\n<p><span style=\"font-size: 14px;\">\u00a0 \u00a0 print(sess.run(w))<\/span><br \/>\n<span style=\"font-size: 14px;\">\u00a0 \u00a0 print(sess.run(b))<\/span><\/p>\n<p><span style=\"font-size: 14px;\">6\u3001\u8fd0\u884c<\/span><\/p>\n<p><span style=\"font-size: 14px;\">python client.py<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p><span style=\"font-size:14px;\">1\u3001\u88c5\u597d\u4e09\u53f0\u865a\u62df\u673atf1, tf2, tf3<\/span><\/p>\n<p><span style=\"font-size:14px;\">\uff08\u7565\uff09<\/span><\/p>\n<p><span style=\"font-size:14px;\">2\u3001\u5728tf1\u4e0a<\/span><\/p>\n<p><span style=\"font-size:14px;\">vim server.py<\/span><\/p>\n<p><span style=\"font-size:14px;\">import tensorflow as tf<\/span><\/p>\n<p><span style=\"font-size:14px;\">worker1 = &#8220;tf1:10000&#8221;<\/span><br \/><span style=\"font-size:14px;\">worker2 = &#8220;tf2:10000&#8221;<\/span><br \/><span style=\"font-size:14px;\">worker3 = &#8220;tf3:10000&#8221;<\/span><\/p>\n<p><span style=\"font-size:14px;\">worker_hosts = [worker1, worker2, worker3]<\/span><br \/><span style=\"font-size:14px;\">cluster_spec = tf.tra&#8230;<\/span><\/p>\n","protected":false},"author":1,"featured_media":565,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[334],"tags":[],"class_list":["post-258","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u642d\u5efa\u6700\u7b80\u6613\u7684TensorFlow\u96c6\u7fa4 - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=258\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u642d\u5efa\u6700\u7b80\u6613\u7684TensorFlow\u96c6\u7fa4 - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"1\u3001\u88c5\u597d\u4e09\u53f0\u865a\u62df\u673atf1, tf2, tf3\uff08\u7565\uff092\u3001\u5728tf1\u4e0avim server.pyimport tensorflow as tfworker1 = &quot;tf1:10000&quot;worker2 = &quot;tf2:10000&quot;worker3 = &quot;tf3:10000&quot;worker_hosts = [worker1, worker2, worker3]cluster_spec = tf.tra...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=258\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-20T11:34:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-30T18:04:19+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/weizn.net\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"569\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/weizn.net\/?p=258#primaryimage\",\"inLanguage\":\"zh-Hans\",\"url\":\"http:\/\/weizn.net\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg\",\"contentUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg\",\"width\":1024,\"height\":569},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=258#webpage\",\"url\":\"http:\/\/weizn.net\/?p=258\",\"name\":\"\\u642d\\u5efa\\u6700\\u7b80\\u6613\\u7684TensorFlow\\u96c6\\u7fa4 - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=258#primaryimage\"},\"datePublished\":\"2018-07-20T11:34:30+00:00\",\"dateModified\":\"2020-09-30T18:04:19+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=258#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=258\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=258#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\\u642d\\u5efa\\u6700\\u7b80\\u6613\\u7684TensorFlow\\u96c6\\u7fa4\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=258#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=258#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"\\u642d\\u5efa\\u6700\\u7b80\\u6613\\u7684TensorFlow\\u96c6\\u7fa4\",\"datePublished\":\"2018-07-20T11:34:30+00:00\",\"dateModified\":\"2020-09-30T18:04:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=258#webpage\"},\"wordCount\":246,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"image\":{\"@id\":\"http:\/\/weizn.net\/?p=258#primaryimage\"},\"thumbnailUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=258#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u642d\u5efa\u6700\u7b80\u6613\u7684TensorFlow\u96c6\u7fa4 - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=258","og_locale":"zh_CN","og_type":"article","og_title":"\u642d\u5efa\u6700\u7b80\u6613\u7684TensorFlow\u96c6\u7fa4 - Wayne&#039;s Blog","og_description":"1\u3001\u88c5\u597d\u4e09\u53f0\u865a\u62df\u673atf1, tf2, tf3\uff08\u7565\uff092\u3001\u5728tf1\u4e0avim server.pyimport tensorflow as tfworker1 = \"tf1:10000\"worker2 = \"tf2:10000\"worker3 = \"tf3:10000\"worker_hosts = [worker1, worker2, worker3]cluster_spec = tf.tra...","og_url":"http:\/\/weizn.net\/?p=258","og_site_name":"Wayne&#039;s Blog","article_published_time":"2018-07-20T11:34:30+00:00","article_modified_time":"2020-09-30T18:04:19+00:00","og_image":[{"width":1024,"height":569,"url":"http:\/\/weizn.net\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg","path":"\/app\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg","size":"full","id":565,"alt":"","pixels":582656,"type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"ImageObject","@id":"http:\/\/weizn.net\/?p=258#primaryimage","inLanguage":"zh-Hans","url":"http:\/\/weizn.net\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg","contentUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg","width":1024,"height":569},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=258#webpage","url":"http:\/\/weizn.net\/?p=258","name":"\u642d\u5efa\u6700\u7b80\u6613\u7684TensorFlow\u96c6\u7fa4 - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/weizn.net\/?p=258#primaryimage"},"datePublished":"2018-07-20T11:34:30+00:00","dateModified":"2020-09-30T18:04:19+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=258#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=258"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=258#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"\u642d\u5efa\u6700\u7b80\u6613\u7684TensorFlow\u96c6\u7fa4"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=258#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=258#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"\u642d\u5efa\u6700\u7b80\u6613\u7684TensorFlow\u96c6\u7fa4","datePublished":"2018-07-20T11:34:30+00:00","dateModified":"2020-09-30T18:04:19+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=258#webpage"},"wordCount":246,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"image":{"@id":"http:\/\/weizn.net\/?p=258#primaryimage"},"thumbnailUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2018\/07\/tensorflow-1024x569-1.jpg","articleSection":["Python"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=258#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=258"}],"version-history":[{"count":1,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/258\/revisions"}],"predecessor-version":[{"id":566,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/258\/revisions\/566"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/media\/565"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}