为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

梵蒂冈之花 2023-06-27 16:21:28
我正在尝试在我的 Tensorflow 代码中实现分布式执行。我创建了一个简单的例子。当我运行它时,该程序不会产生任何结果。我的猜测是我的 Linux 系统的主机位置设置不正确。import tensorflow as tfcluster = tf.train.ClusterSpec({"local": ["localhost:2222", "localhost:2223"]})x = tf.constant(2)with tf.device("/job:local/task:1"):    y2 = x - 66with tf.device("/job:local/task:0"):    y1 = x + 300    y = y1 + y2with tf.Session("grpc://localhost:2222") as sess:    result = sess.run(y)    print(result) 
查看完整描述

1 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

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

查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 81 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信