def test_load(self): vocab = self.vocab seq = ["i", "like", "python"] vocab.add_sequence(seq) file_name = "vocab_file" vocab.save(file_name) loaded_vocab = Vocabulary.load(file_name) os.remove(file_name) self.assertEqual(vocab, loaded_vocab)
def load(cls, path): """ Loads a Checkpoint object that was previously saved to disk. Args: path (str): path to the checkpoint subdirectory Returns: checkpoint (Checkpoint): checkpoint object with fields copied from those stored on disk """ print("Loading checkpoints from {}".format(path)) resume_checkpoint = torch.load( os.path.join(path, cls.TRAINER_STATE_NAME)) model = torch.load(os.path.join(path, cls.MODEL_NAME)) input_vocab = Vocabulary.load(os.path.join(path, cls.INPUT_VOCAB_FILE)) output_vocab = Vocabulary.load( os.path.join(path, cls.OUTPUT_VOCAB_FILE)) return Checkpoint(model=model, input_vocab=input_vocab, output_vocab=output_vocab, optimizer_state_dict=resume_checkpoint['optimizer'], epoch=resume_checkpoint['epoch'], step=resume_checkpoint['step'], path=path)
def test_load(self): vocab = self.vocab seq = ["i", "like", "python"] vocab.add_sequence(seq) pickle_file = "vocab_pickle" input_vocab_pickle = pickle.dumps(vocab) with open(pickle_file, "wb") as f: f.write(input_vocab_pickle) with open(pickle_file, "rb") as f: pickled_vocab = pickle.load(f) loaded_vocab = Vocabulary.load(pickle_file) self.assertEqual(pickled_vocab, loaded_vocab)