【问题标题】:Parallel programming in Tensorflow code with Session.run使用 Session.run 在 Tensorflow 代码中进行并行编程
【发布时间】:2020-12-15 17:13:12
【问题描述】:

我正在尝试在我的 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 tensorflow parallel-processing cluster-computing grpc


    【解决方案1】:

    在运行上述会话之前,需要使用另一个脚本(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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      相关资源
      最近更新 更多