コード例 #1
0
 def write_tsv(records: Iterator[DialogRecord], path: Union[str, Path]):
     seqs = ((str(x), ' '.join(map(str, y))) for x, y in records)
     lines = (f'{x}\t{y}\n' for x, y in seqs)
     log.info(f"Storing data at {path}")
     with IO.writer(path) as f:
         for line in lines:
             f.write(line)
コード例 #2
0
    def store_model(self, step: int, model, train_score: float,
                    val_score: float, keep: int):
        """
        saves model to a given path
        :param step: step number of training
        :param model: model object itself
        :param train_score: score of model on training split
        :param val_score: score of model on validation split
        :param keep: number of good models to keep, bad models will be deleted
        :return:
        """
        # TODO: improve this by skipping the model save if the model is not good enough to be saved
        if self.read_only:
            log.warning("Ignoring the store request; experiment is readonly")
            return
        name = f'model_{step:05d}_{train_score:.6f}_{val_score:.6f}.pkl'
        path = self.model_dir / name
        log.info(f"Saving... step={step} to {path}")
        torch.save(model, str(path))

        for bad_model in self.list_models(sort='total_score',
                                          desc=False)[keep:]:
            log.info(f"Deleting bad model {bad_model} . Keep={keep}")
            os.remove(str(bad_model))

        with IO.writer(os.path.join(self.model_dir, 'scores.tsv'),
                       append=True) as f:
            cols = [
                str(step),
                datetime.now().isoformat(), name, f'{train_score:g}',
                f'{val_score:g}'
            ]
            f.write('\t'.join(cols) + '\n')
コード例 #3
0
 def write_lines(path: Union[str, Path], lines):
     count = 0
     with IO.writer(path) as out:
         for line in lines:
             count += 1
             out.write(line.strip())
             out.write("\n")
         log.info(f"Wrote {count} lines to {path}")
コード例 #4
0
 def write_dialogs(dialogs: Iterator[Dialog], out: Path, dialog_sep='\n'):
     count = 0
     with IO.writer(out) as outh:
         for dialog in dialogs:
             count += 1
             for utter in dialog.chat:
                 if utter.uid:
                     outh.write(f'{utter.uid}\t')
                 if utter.weight:
                     outh.write(f'{utter.weight:g}\t')
                 text = " ".join(map(str, utter.text))
                 outh.write(f'{utter.char}\t{text}\n')
             outh.write(dialog_sep)
     log.info(f"Wrote {count} recs to {out}")
コード例 #5
0
 def store_config(self):
     with IO.writer(self._config_file) as fp:
         return yaml.dump(self.config, fp, default_flow_style=False)