Hi, my LightningModule contains an attribute x which I ignore in save_hyperparameters. As expected, when I try using LightningModule.load_from_checkpoint, it asks to pass x in as kwargs. However, when I use the same checkpoint path as the value for resume_from_checkpoint in a trainer, I am not being asked for x, and everything appears to work just fine- it seems to “remember” x from somewhere else. Not to look a gift horse in the mouth, but I’d like to know- why do these two options behave differently? Thanks!
This is because
.load_from_checkpoint reinstantiates your model with the hyperparameters from the checkpoint. It creates a new instance by calling the constructor.
Trainer(resume_from_checkpoint=...) only loads the weights. Btw, in new versions it is
At the time the loading is executed, the model is already instantiated:
model = ... # this is the instance trainer.fit(ckpt_path=...) # load weights from checkpoint and resume
I’d like to know- why do these two options behave differently?
So in summary, they behave differently because they are not meant for the same use case:
- The mechanism on the trainer is for resuming the training
- The loading mechanism on LM is a convenience to load a model only (for example for testing, inference etc.).