使用 Session.run 在 Tensorflow 代码中进行并行编程

Parallel programming in Tensorflow code with Session.run

我正在尝试在我的 Tensorflow 代码中实现分布式执行。我创建了一个简单的例子。当我 运行 它时,程序不会产生任何结果。我的猜测是我的 Linux 系统的主机位置设置不正确。

import tensorflow as tf


cluster = tf.train.ClusterSpec({"local": ["localhost:2222", "localhost:2223"]})

x = tf.constant(2)


with tf.device("/job:local/task:1"):
    y2 = x - 66

with tf.device("/job:local/task:0"):
    y1 = x + 300
    y = y1 + y2


with tf.Session("grpc://localhost:2222") as sess:
    result = sess.run(y)
    print(result) 

在上面的运行会话之前,需要用另一个脚本启动2个worker(python tfserver.py 0 & python tfserver.py 1)。此外,由于集群中的某些限制,我不得不将 localhost 替换为实际的服务器名称。

# Get task number from command line
import sys
task_number = int(sys.argv[1])

import tensorflow as tf

cluster = tf.train.ClusterSpec({"local": ["localhost:2222", "localhost:2223"]})
server = tf.train.Server(cluster, job_name="local", task_index=task_number)

print("Starting server #{}".format(task_number))

server.start()
server.join()

来源:https://databricks.com/tensorflow/distributed-computing-with-tensorflow

更高级的用法在这里:https://github.com/tensorflow/examples/blob/master/community/en/docs/deploy/distributed.md