Repository: OpenNMT/OpenNMT-py

Issue:
-param_init does not initialize generator
Simple fix, unless this is intentional. 

In ModelConstructor, param_init is done before the generator is assigned to the model, so the generator params aren't initialized.

report_stats is being reset after every batch (ignoring report_every)
After every batch, report_stats = Statistics() is executed in Trainer.py, so its not aggregating the stats anymore. That line should be moved into report_func or something.


Discussion:

@JianyuZhan can you check this.

Resolving patch:
diff --git a/onmt/ModelConstructor.py b/onmt/ModelConstructor.py
--- a/onmt/ModelConstructor.py
+++ b/onmt/ModelConstructor.py
@@ -168,15 +168,17 @@ def make_base_model(model_opt, fields, gpu, checkpoint=None):
         generator.load_state_dict(checkpoint['generator'])
     else:
         if model_opt.param_init != 0.0:
-            print('Intializing parameters.')
+            print('Intializing model parameters.')
             for p in model.parameters():
                 p.data.uniform_(-model_opt.param_init, model_opt.param_init)
+            for p in generator.parameters():
+                p.data.uniform_(-model_opt.param_init, model_opt.param_init)
         model.encoder.embeddings.load_pretrained_vectors(
                 model_opt.pre_word_vecs_enc, model_opt.fix_word_vecs_enc)
         model.decoder.embeddings.load_pretrained_vectors(
                 model_opt.pre_word_vecs_dec, model_opt.fix_word_vecs_dec)
 
-    # add the generator to the module (does this register the parameter?)
+    # Add generator to model (this registers it as parameter of model).
     model.generator = generator
 
     # Make the whole model leverage GPU if indicated to do so.
diff --git a/onmt/Trainer.py b/onmt/Trainer.py
--- a/onmt/Trainer.py
+++ b/onmt/Trainer.py
@@ -133,10 +133,9 @@ def train(self, epoch, report_func=None):
                     dec_state.detach()
 
             if report_func is not None:
-                report_func(epoch, i, len(self.train_iter),
-                            total_stats.start_time, self.optim.lr,
-                            report_stats)
-                report_stats = Statistics()
+                report_stats = report_func(
+                        epoch, i, len(self.train_iter),
+                        total_stats.start_time, self.optim.lr, report_stats)
 
         return total_stats
 
diff --git a/train.py b/train.py
--- a/train.py
+++ b/train.py
@@ -67,18 +67,24 @@ def report_func(epoch, batch, num_batches,
     """
     This is the user-defined batch-level traing progress
     report function.
+
     Args:
         epoch(int): current epoch count.
         batch(int): current batch count.
         num_batches(int): total number of batches.
         start_time(float): last report time.
         lr(float): current learning rate.
-        report_stats(Statistics): a Statistics instance.
+        report_stats(Statistics): old Statistics instance.
+    Returns:
+        report_stats(Statistics): updated Statistics instance.
     """
     if batch % opt.report_every == -1 % opt.report_every:
         report_stats.output(epoch, batch+1, num_batches, start_time)
         if opt.exp_host:
             report_stats.log("progress", experiment, lr)
+        report_stats = onmt.Statistics()
+
+    return report_stats
 
 
 def make_train_data_iter(train_data, opt):


Test patch: