| 16 | |
| 17 | |
| 18 | def train_and_validate(cfg, solver): |
| 19 | if cfg.train.num_epoch == 0: |
| 20 | return |
| 21 | |
| 22 | step = math.ceil(cfg.train.num_epoch / 50) |
| 23 | best_result = float("-inf") |
| 24 | best_epoch = -1 |
| 25 | |
| 26 | for i in range(0, cfg.train.num_epoch, step): |
| 27 | kwargs = cfg.train.copy() |
| 28 | kwargs["num_epoch"] = min(step, cfg.train.num_epoch - i) |
| 29 | solver.train(**kwargs) |
| 30 | solver.save("model_epoch_%d.pth" % solver.epoch) |
| 31 | metric = solver.evaluate("valid") |
| 32 | result = metric[cfg.metric] |
| 33 | if result > best_result: |
| 34 | best_result = result |
| 35 | best_epoch = solver.epoch |
| 36 | |
| 37 | solver.load("model_epoch_%d.pth" % best_epoch) |
| 38 | return solver |
| 39 | |
| 40 | |
| 41 | if __name__ == "__main__": |