Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. import argparse
  2. import logging
  3.  
  4. import tensorflow as tf
  5.  
  6. log = logging.getLogger(__name__)
  7.  
  8. # Job Names
  9. PARAMETER_SERVER = "ps"
  10. WORKER_SERVER = "worker"
  11.  
  12. # Cluster Details
  13. CLUSTER_SPEC = {
  14. PARAMETER_SERVER: ["localhost:2222"],
  15. WORKER_SERVER: ["localhost:1111", "localhost:1112"]}
  16.  
  17.  
  18. def parse_command_arguments():
  19. """ Set up and parse the command line arguments passed for experiment. """
  20. parser = argparse.ArgumentParser(
  21. description="Parameters and Arguments for the Test.")
  22. parser.add_argument(
  23. "--job_name",
  24. type=str,
  25. default="",
  26. help="One of 'ps', 'worker'"
  27. )
  28. # Flags for defining the tf.train.Server
  29. parser.add_argument(
  30. "--task_index",
  31. type=int,
  32. default=0,
  33. help="Index of task within the job"
  34. )
  35.  
  36. return parser.parse_args()
  37.  
  38.  
  39. def start_server(job_name, task_index):
  40. """ Create a server based on a cluster spec. """
  41. cluster = tf.train.ClusterSpec(CLUSTER_SPEC)
  42. server = tf.train.Server(
  43. cluster, job_name=job_name, task_index=task_index)
  44.  
  45. return server, cluster
  46.  
  47.  
  48. def model():
  49. """ Build up a simple estimator model. """
  50. # Build a linear model and predict values
  51. W = tf.Variable([.3], tf.float32)
  52. b = tf.Variable([-.3], tf.float32)
  53. x = tf.placeholder(tf.float32)
  54. linear_model = W * x + b
  55. y = tf.placeholder(tf.float32)
  56. global_step = tf.get_variable('global_step', [],
  57. initializer=tf.constant_initializer(0),
  58. trainable=False)
  59.  
  60. # Loss sub-graph
  61. loss = tf.reduce_sum(tf.square(linear_model - y))
  62.  
  63. # optimizer
  64. optimizer = tf.train.GradientDescentOptimizer(0.01)
  65. train = optimizer.minimize(loss, global_step=global_step)
  66.  
  67. init_op = tf.global_variables_initializer()
  68. log.info("Variables initialized ...")
  69.  
  70. return W, b, loss, x, y, train, global_step, init_op
  71.  
  72.  
  73. if __name__ == "__main__":
  74. # Initializing logging with level "INFO".
  75. logging.basicConfig(level=logging.INFO)
  76.  
  77. # Parse arguments from command line.
  78. arguments = parse_command_arguments()
  79. job_name = arguments.job_name
  80. task_index = arguments.task_index
  81.  
  82. # Start a server.
  83. server, cluster = start_server(job_name, task_index)
  84.  
  85. if job_name == "ps":
  86. server.join()
  87. else:
  88. with tf.device(tf.train.replica_device_setter(
  89. worker_device="/job:worker/task:%d" % task_index,
  90. cluster=cluster)):
  91. W, b, loss, x, y, train, global_step, init_op = model()
  92. with tf.train.MonitoredTrainingSession(
  93. master=server.target,
  94. is_chief=(arguments.task_index == 0 and (
  95. arguments.job_name == 'worker'))) as sess:
  96. step = 0
  97. # training data
  98. x_train = [1, 2, 3, 4]
  99. y_train = [0, -1, -2, -3]
  100. while not sess.should_stop() and step < 1000:
  101. _, step = sess.run(
  102. [train, global_step], {x: x_train, y: y_train})
  103.  
  104. # evaluate training accuracy
  105. curr_W, curr_b, curr_loss = sess.run(
  106. [W, b, loss], {x: x_train, y: y_train})
  107. print("W: %s b: %s loss: %s" % (curr_W, curr_b, curr_loss))
  108.  
  109. $ python test.py --task_index 1 --job_name worker
  110. I tensorflow/core/distributed_runtime/rpc/grpc_channel.cc:197] Initialize GrpcChannelCache for job ps -> {0 -> localhost:2222}
  111. I tensorflow/core/distributed_runtime/rpc/grpc_channel.cc:197] Initialize GrpcChannelCache for job worker -> {0 -> localhost:1111, 1 -> localhost:1112}
  112. I tensorflow/core/distributed_runtime/rpc/grpc_server_lib.cc:211] Started server with target: grpc://localhost:1112
  113. INFO:__main__:Variables initialized ...
  114. I tensorflow/core/distributed_runtime/master_session.cc:993] Start master session 9912c75f2921fe13 with config:
  115.  
  116. INFO:tensorflow:Waiting for model to be ready. Ready_for_local_init_op: None, ready: Variables not initialized: Variable, Variable_1, global_step
  117. INFO:tensorflow:Waiting for model to be ready. Ready_for_local_init_op: None, ready: Variables not initialized: Variable, Variable_1, global_step
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement