Beispiel #1
0
 def compute_test_loss():
     log.info("computing test loss")
     test_loss = compute_loss_all(encdec, test_data, eos_idx, mb_size, gpu=gpu,
                                  reverse_src=reverse_src, reverse_tgt=reverse_tgt,
                                  use_chainerx=use_chainerx)
     log.info("test loss: %f" % test_loss)
     return test_loss
Beispiel #2
0
 def compute_valid_loss():
     log.info("computing valid loss")
     dev_loss = compute_loss_all(encdec, valid_data, eos_idx, mb_size, gpu=gpu,
                                 reverse_src=reverse_src, reverse_tgt=reverse_tgt,
                                 use_chainerx=use_chainerx)
     log.info("valid loss: %f" % dev_loss)
     return dev_loss
Beispiel #3
0
    def __call__(self, trainer):
        encdec = trainer.updater.get_optimizer("main").target
        log.info("computing %s" % self.observation_name)
        dev_loss = compute_loss_all(encdec, self.data, self.eos_idx, self.mb_size,
                                    gpu=self.gpu,
                                    reverse_src=self.reverse_src, reverse_tgt=self.reverse_tgt,
                                    use_chainerx=self.use_chainerx)
        log.info("%s: %f (current best: %r)" % (self.observation_name, dev_loss, self.best_loss))
        chainer.reporter.report({self.observation_name: dev_loss})

        if self.best_loss is None or self.best_loss > dev_loss:
            log.info("loss (%s) improvement: %r -> %r" % (self.observation_name,
                                                          self.best_loss, dev_loss))
            self.best_loss = dev_loss
            if self.save_best_model_to is not None:
                log.info("saving best loss (%s) model to %s" % (self.observation_name, self.save_best_model_to,))
                serializers.save_npz(self.save_best_model_to, encdec)
                if self.config_training is not None:
                    config_session = self.config_training.copy(readonly=False)
                    config_session.add_section("model_parameters", keep_at_bottom="metadata")
                    config_session["model_parameters"]["filename"] = self.save_best_model_to
                    config_session["model_parameters"]["type"] = "model"
                    config_session["model_parameters"]["description"] = "best_loss"
                    config_session["model_parameters"]["infos"] = argument_parsing_tools.OrderedNamespace()
                    config_session["model_parameters"]["infos"]["loss"] = float(dev_loss)
                    config_session["model_parameters"]["infos"]["iteration"] = trainer.updater.iteration
                    config_session.set_metadata_modified_time()
                    config_session.save_to(self.save_best_model_to + ".config")
Beispiel #4
0
 def compute_dev_loss():
     log.info("computing dev loss")
     dev_loss = compute_loss_all(encdec, dev_data, eos_idx, mb_size, gpu=gpu,
                                 reverse_src=reverse_src, reverse_tgt=reverse_tgt)
     log.info("dev loss: %f" % dev_loss)
     return dev_loss