I have various python machine learning models/classes that have the following class defs:
class Model(pl.LightningModule):
def __init__(self, **kwargs):
# perform initialisation
I am using pytorch lightning which also allows one to create a model instance from a saved checkpoint.
For each class I provide a load method as:
def load(**kwargs):
if "checkpoint_file" in kwargs:
# call the class method provided by the parent
return Model.load_from_checkpoint(kwargs["checkpoint_file"])
else:
return MyModel(**kwargs)
I need to do this for every class as I need to provide a single callable instansiator (due to use of facebook hydra). I was wondering if there is some design pattern I can utilise to not have to define this for every class.
Aucun commentaire:
Enregistrer un commentaire