| Conditions | 19 |
| Total Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like MultiGPUTrainer.train() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #!/usr/bin/env python |
||
| 37 | def train(self, train_set, valid_set=None, test_set=None, train_size=None): |
||
| 38 | """ |
||
| 39 | Train the model in multi-GPU environment. |
||
| 40 | """ |
||
| 41 | server_port = self.config.get("server_port", 5567) |
||
| 42 | param_map = self.create_param_map() |
||
| 43 | # Initialize the worker |
||
| 44 | worker = Worker(control_port=server_port) |
||
| 45 | self.sync_hyperparams(worker.send_req('sync_hyperparams')['sync_hyperparams']) |
||
| 46 | easgd_alpha = worker.send_req('get_easgd_alpha') |
||
| 47 | worker.init_shared_params(param_map.values(), param_sync_rule=EASGD(easgd_alpha)) |
||
| 48 | worker.copy_to_local() |
||
| 49 | # Load all training batches, consume vast memory here |
||
| 50 | self.logger.info("started process {}".format(os.getpid())) |
||
| 51 | self.logger.info("(proc {}) load training data".format(os.getpid())) |
||
| 52 | train_batches = list(train_set) |
||
| 53 | network_callback = bool(self.network.training_callbacks) |
||
| 54 | trainer_callback = bool(self._iter_callbacks) |
||
| 55 | while True: |
||
| 56 | resp = worker.send_req('next') |
||
| 57 | if resp == 'stop': |
||
| 58 | break |
||
| 59 | elif resp == 'wait': |
||
| 60 | time.sleep(1) |
||
| 61 | elif resp == 'get_num_batches': |
||
| 62 | worker.send_req({'get_num_batches_done': len(train_batches)}) |
||
| 63 | elif resp == 'eval': |
||
| 64 | worker.copy_to_local() |
||
| 65 | messages = [] |
||
| 66 | if valid_set: |
||
| 67 | self._run_valid(self.epoch, valid_set) |
||
| 68 | messages.append(self.network.train_logger.log_pool[-1]) |
||
| 69 | if test_set: |
||
| 70 | self._run_test(self.epoch, test_set) |
||
| 71 | messages.append(self.network.train_logger.log_pool[-1]) |
||
| 72 | worker.send_req({"eval_done": messages}) |
||
| 73 | elif resp == 'valid': |
||
| 74 | worker.copy_to_local() |
||
| 75 | messages = [] |
||
| 76 | if valid_set: |
||
| 77 | # TODO: set and send the best cost |
||
| 78 | self._run_valid(self.epoch, valid_set, dry_run=True) |
||
| 79 | messages.append(self.network.train_logger.log_pool[-1]) |
||
| 80 | worker.send_req({"valid_done": messages}) |
||
| 81 | elif 'train' in resp: |
||
| 82 | batch_ids = resp['train'] |
||
| 83 | batch_costs = [[] for _ in self.training_names] |
||
| 84 | for batch_id in batch_ids: |
||
| 85 | x = train_batches[batch_id] |
||
| 86 | cost_x = self.learn(*x) |
||
| 87 | for i, cost in enumerate(cost_x): |
||
| 88 | batch_costs[i].append(cost) |
||
| 89 | self.last_cost = cost_x[0] |
||
| 90 | if network_callback: |
||
| 91 | self.network.training_callback() |
||
| 92 | if trainer_callback: |
||
| 93 | for func in self._iter_callbacks: |
||
| 94 | func(self) |
||
| 95 | worker.sync_params(synchronous=True) |
||
| 96 | worker.send_req({'train_done': None, 'costs': [float(np.mean(c)) for c in batch_costs]}) |
||
| 97 | elif 'sync_hyperparams' in resp: |
||
| 98 | self.sync_hyperparams(resp['sync_hyperparams']) |
||
| 99 | worker.close() |
||
| 100 | return [] |
||
| 101 |