def handle_control(self, req, worker_id, req_info):
if self.start_time is None: self.start_time = time.time()<EOL>response = "<STR_LIT>"<EOL>if req == '<STR_LIT>':<EOL><INDENT>if self.num_train_batches == <NUM_LIT:0>:<EOL><INDENT>response = "<STR_LIT>"<EOL><DEDENT>elif self._done:<EOL><INDENT>response = "<STR_LIT>"<EOL>self.worker_is_done(worker_id)<EOL><DEDENT>elif self._evaluating:<EOL><INDENT>response = '<STR_LIT>'<EOL><DEDENT>elif not self.batch_pool:<EOL><INDENT>if self._train_costs:<EOL><INDENT>with self._lock:<EOL><INDENT>sys.stdout.write("<STR_LIT:\r>")<EOL>sys.stdout.flush()<EOL>mean_costs = []<EOL>for i in range(len(self._training_names)):<EOL><INDENT>mean_costs.append(np.mean([c[i] for c in self._train_costs]))<EOL><DEDENT>self.log("<STR_LIT>".format(<EOL>self.epoch,<EOL>self.get_monitor_string(zip(self._training_names, mean_costs)))<EOL>)<EOL><DEDENT><DEDENT>response = {'<STR_LIT>': None, '<STR_LIT>': self._best_valid_cost}<EOL>self._evaluating = True<EOL><DEDENT>else:<EOL><INDENT>if worker_id not in self.prepared_worker_pool:<EOL><INDENT>response = {"<STR_LIT>": self.feed_hyperparams()}<EOL>self.prepared_worker_pool.add(worker_id)<EOL><DEDENT>elif self._iters_from_last_valid >= self._valid_freq:<EOL><INDENT>response = {'<STR_LIT>': None, '<STR_LIT>': self._best_valid_cost}<EOL>self._iters_from_last_valid = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>response = {"<STR_LIT:train>": self.feed_batches()}<EOL><DEDENT><DEDENT><DEDENT>elif '<STR_LIT>' in req:<EOL><INDENT>with self._lock:<EOL><INDENT>self._evaluating = False<EOL>sys.stdout.write("<STR_LIT:\r>")<EOL>sys.stdout.flush()<EOL>if '<STR_LIT>' in req and req['<STR_LIT>']:<EOL><INDENT>self.log("<STR_LIT>".format(<EOL>self.epoch,<EOL>self.get_monitor_string(req['<STR_LIT>']),<EOL>worker_id)<EOL>)<EOL><DEDENT>if '<STR_LIT>' in req and req['<STR_LIT>']:<EOL><INDENT>valid_J = req['<STR_LIT>'][<NUM_LIT:0>][<NUM_LIT:1>]<EOL>if valid_J < self._best_valid_cost:<EOL><INDENT>self._best_valid_cost = valid_J<EOL>star_str = "<STR_LIT:*>"<EOL><DEDENT>else:<EOL><INDENT>star_str = "<STR_LIT>"<EOL>self.log("<STR_LIT>".format(<EOL>self.epoch,<EOL>self.get_monitor_string(req['<STR_LIT>']),<EOL>star_str,<EOL>worker_id))<EOL><DEDENT><DEDENT>continue_training = self.prepare_epoch()<EOL>self._epoch_start_time = time.time()<EOL>if not continue_training:<EOL><INDENT>self._done = True<EOL>self.log("<STR_LIT>".format(time.time() - self.start_time))<EOL>response = "<STR_LIT>"<EOL><DEDENT><DEDENT><DEDENT>elif '<STR_LIT>' in req:<EOL><INDENT>with self._lock:<EOL><INDENT>sys.stdout.write("<STR_LIT:\r>")<EOL>sys.stdout.flush()<EOL>if '<STR_LIT>' in req:<EOL><INDENT>valid_J = req['<STR_LIT>'][<NUM_LIT:0>][<NUM_LIT:1>]<EOL>if valid_J < self._best_valid_cost:<EOL><INDENT>self._best_valid_cost = valid_J<EOL>star_str = "<STR_LIT:*>"<EOL><DEDENT>else:<EOL><INDENT>star_str = "<STR_LIT>"<EOL><DEDENT>self.log("<STR_LIT>".format(<EOL>self.get_monitor_string(req['<STR_LIT>']),<EOL>star_str,<EOL>worker_id<EOL>))<EOL><DEDENT><DEDENT><DEDENT>elif '<STR_LIT>' in req:<EOL><INDENT>costs = req['<STR_LIT>']<EOL>self._train_costs.append(costs)<EOL>sys.stdout.write("<STR_LIT>" % (<EOL>self._current_iter * <NUM_LIT:100> / self.num_train_batches,<EOL>costs[<NUM_LIT:0>], float(len(self._train_costs) * self.sync_freq) / (time.time() - self._epoch_start_time)))<EOL>sys.stdout.flush()<EOL><DEDENT>elif '<STR_LIT>' in req:<EOL><INDENT>self.num_train_batches = req['<STR_LIT>']<EOL><DEDENT>elif '<STR_LIT>' in req:<EOL><INDENT>response = self._easgd_alpha<EOL><DEDENT>elif '<STR_LIT>' in req:<EOL><INDENT>response = {"<STR_LIT>": self.feed_hyperparams()}<EOL><DEDENT>elif '<STR_LIT>' in req:<EOL><INDENT>with self._lock:<EOL><INDENT>sys.stdout.write("<STR_LIT:\r>")<EOL>sys.stdout.flush()<EOL>self.log("<STR_LIT>".format(worker_id))<EOL>if self.epoch == <NUM_LIT:0>:<EOL><INDENT>schedule_params = req['<STR_LIT>']<EOL>sch_str = "<STR_LIT:U+0020>".join("<STR_LIT>".format(a, b) for (a, b) in schedule_params.items())<EOL>self.log("<STR_LIT>".format(sch_str))<EOL>for key, val in schedule_params.items():<EOL><INDENT>if not val: continue<EOL>if key == '<STR_LIT>':<EOL><INDENT>self._lr = val<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>self.epoch_start_halving = val<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>self._halving_freq = val<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>self.end_at = val<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>self.sync_freq = val<EOL><DEDENT>elif key == '<STR_LIT>':<EOL><INDENT>self._valid_freq = val<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>elif '<STR_LIT>' in req:<EOL><INDENT>self._training_names = req['<STR_LIT>']<EOL>self._evaluation_names = req['<STR_LIT>']<EOL><DEDENT>return response<EOL>
Handles a control_request received from a worker.
Returns:
    string or dict: response

    'stop' - the worker should quit
    'wait' - wait for 1 second
    'eval' - evaluate on valid and test set to start a new epoch
    'sync_hyperparams' - set learning rate
    'valid' - evaluate on valid and test set, then save the params
    'train' - train next batches