parser.add_argument('--synthesis_sampling',
type = bool, default = True)
+parser.add_argument('--checkpoint_name',
+ type = str, default = 'checkpoint.pth')
+
######################################################################
args = parser.parse_args()
nb_heads = args.nb_heads, nb_blocks = args.nb_blocks, dropout = args.dropout
)
+model.to(device)
+
nb_parameters = sum(p.numel() for p in model.parameters())
log_string(f'nb_parameters {nb_parameters} ({int(nb_parameters/1e6)}M)')
-model.to(device)
-
######################################################################
if args.optim == 'sgd':
else:
raise ValueError(f'Unknown optimizer {args.optim}.')
-for k in range(args.nb_epochs):
+######################################################################
+
+nb_epochs_finished = 0
+
+try:
+ checkpoint = torch.load(args.checkpoint_name, map_location = device)
+ nb_epochs_finished = checkpoint['nb_epochs_finished']
+ model.load_state_dict(checkpoint['model_state'])
+ optimizer.load_state_dict(checkpoint['optimizer_state'])
+ print(f'Checkpoint loaded with {nb_epochs_finished} epochs finished.')
+
+except FileNotFoundError:
+ print('Starting from scratch.')
+
+except:
+ print('Error when loading the checkpoint.')
+ exit(1)
+
+######################################################################
+
+for k in range(nb_epochs_finished, args.nb_epochs):
model.train()
task.produce_results(k, model)
+ checkpoint = {
+ 'nb_epochs_finished': k + 1,
+ 'model_state': model.state_dict(),
+ 'optimizer_state': optimizer.state_dict()
+ }
+
+ torch.save(checkpoint, args.checkpoint_name)
+
######################################################################