| 10 | |
| 11 | |
| 12 | class Checkpoint(object): |
| 13 | def __init__(self, start_epoch=None, start_iter=None, train_loss=None, eval_loss=None, best_val_loss=float("inf"), |
| 14 | prev_val_loss=float("inf"), state_dict=None, optimizer=None, num_no_improv=0, half_lr=False): |
| 15 | self.start_epoch = start_epoch |
| 16 | self.start_iter = start_iter |
| 17 | self.train_loss = train_loss |
| 18 | self.eval_loss = eval_loss |
| 19 | |
| 20 | self.best_val_loss = best_val_loss |
| 21 | self.prev_val_loss = prev_val_loss |
| 22 | |
| 23 | self.state_dict = state_dict |
| 24 | self.optimizer = optimizer |
| 25 | |
| 26 | self.num_no_improv = num_no_improv |
| 27 | self.half_lr = half_lr |
| 28 | |
| 29 | |
| 30 | def save(self, is_best, filename, best_model): |
| 31 | print('Saving checkpoint at "%s"' % filename) |
| 32 | torch.save(self, filename) |
| 33 | if is_best: |
| 34 | print('Saving the best model at "%s"' % best_model) |
| 35 | shutil.copyfile(filename, best_model) |
| 36 | print('\n') |
| 37 | |
| 38 | |
| 39 | def load(self, filename): |
| 40 | # filename : model path |
| 41 | if os.path.isfile(filename): |
| 42 | print('Loading checkpoint from "%s"\n' % filename) |
| 43 | checkpoint = torch.load(filename, map_location='cpu') |
| 44 | |
| 45 | self.start_epoch = checkpoint.start_epoch |
| 46 | self.start_iter = checkpoint.start_iter |
| 47 | self.train_loss = checkpoint.train_loss |
| 48 | self.eval_loss = checkpoint.eval_loss |
| 49 | |
| 50 | self.best_val_loss = checkpoint.best_val_loss |
| 51 | self.prev_val_loss = checkpoint.prev_val_loss |
| 52 | self.state_dict = checkpoint.state_dict |
| 53 | self.optimizer = checkpoint.optimizer |
| 54 | self.num_no_improv = checkpoint.num_no_improv |
| 55 | self.half_lr = checkpoint.half_lr |
| 56 | else: |
| 57 | raise ValueError('No checkpoint found at "%s"' % filename) |
| 58 | |
| 59 | class InstantLayerNorm1d(nn.Module): |
| 60 | def __init__(self, |
nothing calls this directly
no outgoing calls
no test coverage detected