【发布时间】:2017-10-10 13:17:58
【问题描述】:
看我写的代码:
import tensorflow as tf
tf.flags.DEFINE_string('job_name', 'ps', 'worker or ps')
tf.flags.DEFINE_integer('task_index', 0, 'task id')
FLAGS = tf.flags.FLAGS
host = '127.0.0.1:'
cluster = {"ps": [host+'2222'],
"worker": [host+'2223', host+'2224']}
clusterspec = tf.train.ClusterSpec(cluster)
server = tf.train.Server(cluster,
job_name=FLAGS.job_name,
task_index=FLAGS.task_index)
def print_fn():
print('job_name: %s, task_index: %d' % (FLAGS.job_name, FLAGS.task_index))
if FLAGS.job_name == 'ps':
server.join()
elif FLAGS.job_name == 'worker':
with tf.device(tf.train.replica_device_setter(
worker_device="/job:worker/task:%d" % FLAGS.task_index,
cluster=cluster)):
a = tf.Variable(tf.zeros([]), name='a')
b = tf.Variable(tf.zeros([]), name='b')
op = tf.add(a, b)
print(a.device)
print(b.device)
print(op.device)
print(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES))
print_fn()
当我在cmd 中运行python distributed_replicas.py --job_name=worker --task_index=0,但之前没有运行python distributed_replicas.py --job_name=ps --task_index=0 时,程序也可以运行。 a.device 和 b.device 都是 /job:ps/task:0,但是 ps server 不启动,变量 a 和 b 是如何存储在 ps server 上的?而tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)还包含变量a和b,也就是说a和b是在/job:worker/task:0上创建的,虽然它们的设备是/job:ps/task:0,那怎么了? a 和 b 是在哪里创建的?
【问题讨论】:
标签: python tensorflow distributed-computing grpc