Hello,
I want to load a model from a file and determine the model parameters.
model_path = os.path.normpath(args.model)
model = model_path.split(os.sep)[-1]
splitted = model.split("_")
splitted = splitted[0:-3]
section = "_".join(splitted)
model = CorefModel(args.config_file, section) # <- this works
model.load_state_dict(torch.load(args.model)) # <- that not
total_params = sum( param.numel() for param in model.parameters() )
print("The model " + section + " has a parameter weight of " + total_params)
I get the Error AttributeError: ‘CorefModel’ object has no attribute ‘load_state_dict’
The save-Method:
def save_weights(self):
""" Saves trainable models as state dicts. """
to_save: List[Tuple[str, Any]] = \
[(key, value) for key, value in self.trainable.items()
if self.config.bert_finetune or key != "bert"]
to_save.extend(self.optimizers.items())
to_save.extend(self.schedulers.items())
if self.epochs_trained == self.config.train_epochs:
time = datetime.strftime(datetime.now(), "%Y.%m.%d_%H.%M")
path = os.path.join(self.config.data_dir,
f"{self.config.section}"
f"_(e{self.epochs_trained}_{time}).pt")
savedict = {name: module.state_dict() for name, module in to_save}
savedict["epochs_trained"] = self.epochs_trained # type: ignore
torch.save(savedict, path)
Do I need to implement load_state_dict myself? Or does this only work on a gpu? Currently I am testing it on a CPU?