code: def get_workers(num_workers, replicas_to_aggregate, workers):
      sessions = []
      graphs = []
      train_ops = []
      for worker_id in range(num_workers):
          graph = ops.Graph()
          is_chief = worker_id == 0
          with graph.as_default():
              with ops.device("/job:ps/task:0"):
                  global_step = variable_v1.VariableV1(
                      0, name="global_step", trainable=False
                  )
                  var_0 = variable_v1.VariableV1(0.0, name="v0")
              with ops.device("/job:ps/task:1"):
                  var_1 = variable_v1.VariableV1(1.0, name="v1")
                  var_sparse = variable_v1.VariableV1(
                      [[3.0], [4.0]], name="v_sparse"
                  )
  
              with ops.device("/job:worker/task:" + str(worker_id)):
                  grads_0 = constant_op.constant(0.1 + worker_id * 0.2)
                  grads_1 = constant_op.constant(0.9 + worker_id * 0.2)
                  # This is to test against sparse gradients.
                  grads_sparse = indexed_slices.IndexedSlices(
                      constant_op.constant(
                          [0.1 + worker_id * 0.2], shape=[1, 1]
                      ),
                      constant_op.constant([1]),
                      constant_op.constant([2, 1]),
                  )
                  sgd_opt = gradient_descent.GradientDescentOptimizer(
                      2.0
                  )
                  sync_rep_opt = training.SyncReplicasOptimizer(
                      sgd_opt,
                      replicas_to_aggregate=replicas_to_aggregate,
                      total_num_replicas=num_workers,
                  )
                  train_op = [
                      sync_rep_opt.apply_gradients(
                          zip(
                              [grads_0, grads_1, grads_sparse],
                              [var_0, var_1, var_sparse],
                          ),
                          global_step=global_step,
                      )
                  ]
                  sync_replicas_hook = (
                      sync_rep_opt.make_session_run_hook(
                          is_chief, num_tokens=num_workers
                      )
                  )
  
              # Creates MonitoredSession
              session = training.MonitoredTrainingSession(
                  master=workers[worker_id].target,
                  is_chief=is_chief,
                  hooks=[sync_replicas_hook],
              )
  
          sessions.append(session)
          graphs.append(graph)
          train_ops.append(train_op)
  
      return sessions, graphs, train_ops
  
imports: import time
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import indexed_slices
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import test_util
  from tensorflow.python.framework.test_util import create_local_cluster
  from tensorflow.python.ops import variable_v1
  from tensorflow.python.platform import test
  from tensorflow.python.training import adam
  from tensorflow.python.training import gradient_descent
  from tensorflow.python.training import training
label_desc: Use image node_id 1 for calling a global function with example usage: get_workers(num_workers, replicas_to_aggregate, workers) and returns: sessions,  graphs,  train_ops