如何解决使用Session.run在Tensorflow代码中进行并行编程
我正在尝试在我的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)
解决方法
在运行以上会话之前,需要使用另一个脚本(python tfserver.py 0
和python tfserver.py 1
)启动2个工作进程。另外,由于集群中的某些限制,我不得不用实际的服务器名称替换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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。