forked from OpenNMT/OpenNMT-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving code from train.py and introducing ModelSaver and ReportManage…
…r classes
- Loading branch information
Showing
12 changed files
with
545 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,3 @@ | ||
""" Main entry point of the ONMT library """ | ||
from onmt import decoders | ||
from onmt import encoders | ||
from onmt import inputters | ||
from onmt import models | ||
from onmt import modules | ||
#import onmt.opts | ||
from onmt.trainer import Trainer, Statistics | ||
#from onmt.encoders.transformer import TransformerEncoder | ||
import onmt.model_builder | ||
from onmt.trainer import Trainer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import torch.nn as nn | ||
|
||
import onmt.inputters | ||
|
||
|
||
def build_model_saver(model_opt, opt, model, fields, optim): | ||
model_saver = ModelSaver(opt.save_model, | ||
model, | ||
model_opt, | ||
fields, | ||
optim, | ||
opt.start_checkpoint_at) | ||
return model_saver | ||
|
||
|
||
class ModelSaverBase(object): | ||
def __init__(self, base_path, model, model_opt, fields, optim, start_checkpoint_at=0): | ||
self.base_path = base_path | ||
self.model = model | ||
self.model_opt = model_opt | ||
self.fields = fields | ||
self.optim = optim | ||
self.start_checkpoint_at = start_checkpoint_at | ||
|
||
def maybe_save(self, epoch, valid_stats): | ||
if epoch >= start_checkpoint_at: | ||
self._save(epoch, valid_stats) | ||
|
||
def force_save(self, epoch, valid_stats): | ||
self._save(epoch, valid_stats) | ||
|
||
def _save(self, epoch, valid_stats): | ||
""" Save a resumable checkpoint. | ||
Args: | ||
epoch (int): epoch number | ||
valid_stats : statistics of last validation run | ||
""" | ||
raise NotImplementedError() | ||
|
||
|
||
class ModelSaver(ModelSaverBase): | ||
def __init__(self, base_path, model, model_opt, fields, optim, start_checkpoint_at=0): | ||
super(ModelSaver, self).__init__( | ||
base_path, model, model_opt, fields, optim, start_checkpoint_at=0) | ||
|
||
def _save(self, epoch, valid_stats): | ||
real_model = (self.model.module | ||
if isinstance(self.model, nn.DataParallel) | ||
else self.model) | ||
real_generator = (real_model.generator.module | ||
if isinstance(real_model.generator, nn.DataParallel) | ||
else real_model.generator) | ||
|
||
model_state_dict = real_model.state_dict() | ||
model_state_dict = {k: v for k, v in model_state_dict.items() | ||
if 'generator' not in k} | ||
generator_state_dict = real_generator.state_dict() | ||
checkpoint = { | ||
'model': model_state_dict, | ||
'generator': generator_state_dict, | ||
'vocab': onmt.inputters.save_fields_to_vocab(fields), | ||
'opt': opt, | ||
'epoch': epoch, | ||
'optim': self.optim, | ||
} | ||
torch.save(checkpoint, | ||
'%s_acc_%.2f_ppl_%.2f_e%d.pt' | ||
% (self.base_path, valid_stats.accuracy(), | ||
valid_stats.ppl(), epoch)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""Module defining various utilities.""" | ||
from onmt.utils.misc import aeq | ||
from onmt.utils.loss import NMTLossCompute | ||
from onmt.utils.optimizers import Optimizer | ||
from onmt.utils.misc import aeq, use_gpu | ||
from onmt.utils.report_manager import ReportMgr, build_report_manager | ||
from onmt.utils.statistics import Statistics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.