1、装好三台虚拟机tf1, tf2, tf3

(略)


2、在tf1上


vim server.py


import tensorflow as tf

worker1 = "tf1:10000"

worker2 = "tf2:10000"

worker3 = "tf3:10000"

worker_hosts = [worker1, worker2, worker3]

cluster_spec = tf.train.ClusterSpec({ "worker": worker_hosts})

server = tf.train.Server(cluster_spec, job_name="worker", task_index=0)

server.join()


3、在tf2上


vim server.py


import tensorflow as tf

worker1 = "tf1:10000"

worker2 = "tf2:10000"

worker3 = "tf3:10000"

worker_hosts = [worker1, worker2, worker3]

cluster_spec = tf.train.ClusterSpec({ "worker": worker_hosts})

server = tf.train.Server(cluster_spec, job_name="worker", task_index=1)

server.join()


4、在tf3上


vim server.py


import tensorflow as tf

worker1 = "tf1:10000"

worker2 = "tf2:10000"

worker3 = "tf3:10000"

worker_hosts = [worker1, worker2, worker3]

cluster_spec = tf.train.ClusterSpec({ "worker": worker_hosts})

server = tf.train.Server(cluster_spec, job_name="worker", task_index=2)

server.join()


5、在任意一台tfx主机上


vim client.py


import tensorflow as tf

import numpy as np


train_X = np.linspace(-1,1,1000000)

train_Y = 2*train_X + np.random.randn(*train_X.shape)*0.33+10


X = tf.placeholder("float")

Y = tf.placeholder("float")


w = tf.Variable(0.0, name="weight")

b = tf.Variable(0.0, name="reminder")


init_op = tf.initialize_all_variables()

cost_op = tf.square(Y - tf.mul(X,w) - b)

train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost_op)


with tf.Session("grpc://tf1:10000") as sess:

  with tf.device("/job:worker/task:0"):

    sess.run(init_op)


    for i in range(10):

      for (x, y) in zip(train_X, train_Y):

        sess.run(train_op, feed_dict={X:x, Y:y})

    

    print(sess.run(w))

    print(sess.run(b))


6、运行

python client.py