(job_name, task_index, step, lock)
| 21 | |
| 22 | |
| 23 | def work(job_name, task_index, step, lock): |
| 24 | # set work's ip:port, parameter server and worker are the same steps |
| 25 | cluster = tf.train.ClusterSpec({ |
| 26 | "ps": ['localhost:2221', ], |
| 27 | "worker": ['localhost:2222', 'localhost:2223', 'localhost:2224',] |
| 28 | }) |
| 29 | server = tf.train.Server(cluster, job_name=job_name, task_index=task_index) |
| 30 | |
| 31 | if job_name == 'ps': |
| 32 | # join parameter server |
| 33 | print('Start Parameter Server: ', task_index) |
| 34 | server.join() |
| 35 | else: |
| 36 | print('Start Worker: ', task_index, 'pid: ', mp.current_process().pid) |
| 37 | # worker job |
| 38 | with tf.device(tf.train.replica_device_setter( |
| 39 | worker_device="/job:worker/task:%d" % task_index, |
| 40 | cluster=cluster)): |
| 41 | # build network |
| 42 | tf_x = tf.placeholder(tf.float32, x.shape) |
| 43 | tf_y = tf.placeholder(tf.float32, y.shape) |
| 44 | l1 = tf.layers.dense(tf_x, 10, tf.nn.relu) |
| 45 | output = tf.layers.dense(l1, 1) |
| 46 | loss = tf.losses.mean_squared_error(tf_y, output) |
| 47 | global_step = tf.train.get_or_create_global_step() |
| 48 | train_op = tf.train.GradientDescentOptimizer( |
| 49 | learning_rate=0.001).minimize(loss, global_step=global_step) |
| 50 | |
| 51 | # set training steps |
| 52 | hooks = [tf.train.StopAtStepHook(last_step=100000)] |
| 53 | |
| 54 | # get session |
| 55 | with tf.train.MonitoredTrainingSession(master=server.target, |
| 56 | is_chief=(task_index == 0), |
| 57 | checkpoint_dir='./tmp', |
| 58 | hooks=hooks) as mon_sess: |
| 59 | print("Start Worker Session: ", task_index) |
| 60 | while not mon_sess.should_stop(): |
| 61 | # train |
| 62 | _, loss_ = mon_sess.run([train_op, loss], {tf_x: x, tf_y: y}) |
| 63 | with lock: |
| 64 | step.value += 1 |
| 65 | if step.value % 500 == 0: |
| 66 | print("Task: ", task_index, "| Step: ", step.value, "| Loss: ", loss_) |
| 67 | print('Worker Done: ', task_index) |
| 68 | |
| 69 | |
| 70 | def parallel_train(): |
nothing calls this directly
no outgoing calls
no test coverage detected