'weights': model.state_dict() } if valid_loss < best_loss: filename = os.path.join(args.savedir, 'checkpoint_best.pt') torch.save(save_vars, filename) if epoch % args.save_epoch == 0: filename = os.path.join(args.savedir, f'checkpoint_{epoch}.pt') torch.save(save_vars, filename) filename = os.path.join(args.savedir, 'checkpoint_last.pt') torch.save(save_vars, filename) # update trainer.scheduler.step(valid_loss) epoch += 1 if __name__ == '__main__': parser = argparse.ArgumentParser(''' An Implimentation of Transformer. Attention is all you need. Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. In Advances in Neural Information Processing Systems, pages 6000–6010. ''') options.train_opts(parser) options.model_opts(parser) args = parser.parse_args() main(args)
valid_loss /= len(valid_iter) print(f"| epoch {str(epoch).zfill(3)} | valid ", end="") print(f"| loss {valid_loss:.{4}} ", end="") print(f"| ppl {math.exp(valid_loss):.{4}} ", end="") print(f"| lr {trainer.get_lr():.1e} ", end="") print(f"| clip {args.clip} ", end="") print(f"| num_updates {trainer.n_updates} |") # saving model save_vars = {"train_args": args, "state_dict": model.state_dict()} if valid_loss < best_loss: best_loss = valid_loss save_model(save_vars, 'checkpoint_best.pt') save_model(save_vars, "checkpoint_last.pt") # update trainer.scheduler.step(valid_loss) epoch += 1 if __name__ == '__main__': parser = argparse.ArgumentParser('') train_opts(parser) model_opts(parser) args = parser.parse_args() main(args)
def make_options(): parser = argparse.ArgumentParser(description='train.py') options.model_opts(parser) options.train_opts(parser) return parser.parse_args()