Hello, I am trying to create a pytorch lightning module. I have config folder from which I am creating a hyperparameters dictionary using hydra. When I attempt to set this dictionary to self.hparams
, it returns an attribute error
AttributeError: can't set attribute
I am following the structure from the official pytorch-lightning docs. But I am not sure, why is this error coming.
Here’s my pytorch Lightning module definition
class magNet(LightningModule):
def __init__(self, hparams, *args, **kwargs):
super().__init__()
self.hparams = hparams
print(self.hparams)
self._build_model()
def _build_model(self):
<defining architecture>
def forward(self):
<forward pass>
def training_step(self, batch, batch_idx):
<training step>
In my main()
This function converts all yaml files in the config folder into a single hparams dictionary.
def hydra_params_to_dotdict(hparams):
def _to_dot_dict(cfg):
res = {}
for k, v in cfg.items():
if isinstance(v, omegaconf.DictConfig):
res.update(
{k + "." + subk: subv for subk, subv in _to_dot_dict(v).items()}
)
elif isinstance(v, (str, int, float, bool)):
res[k] = v
return res
return _to_dot_dict(hparams)
@hydra.main("config/config.yaml")
def main(cfg):
params = hydra_params_to_dotdict(cfg)
print(params)
model = magNet(params)
print(model)
main()
Returns
ipython-input-3-48509a4d671d> in main(cfg)
4 print(params)
----> 5 model = magNet(params)
6 print(model)
/media/Data2/ajinkya/codes/magnet_official/magnet/model/arch_torch.py in __init__(self, hparams, *args, **kwargs)
217 super().__init__()
--> 218 self.hparams = hparams
219 print(self.hparams)
~/anaconda3/envs/env/lib/python3.6/site-packages/torch/nn/modules/module.py in __setattr__(self, name, value)
994 else:
--> 995 object.__setattr__(self, name, value)
996
AttributeError: can't set attribute
During handling of the above exception, another exception occurred:
AssertionError Traceback (most recent call last)
<ipython-input-4-263240bbee7e> in <module>
----> 1 main()
~/anaconda3/envs/env/lib/python3.6/site-packages/hydra/main.py in decorated_main(cfg_passthrough)
35 config_path=config_path,
36 config_name=config_name,
---> 37 strict=strict,
38 )
39
~/anaconda3/envs/env/lib/python3.6/site-packages/hydra/_internal/utils.py in _run_hydra(args_parser, task_function, config_path, config_name, strict)
345 if args.run:
346 run_and_report(
--> 347 lambda: hydra.run(
348 config_name=config_name,
349 task_function=task_function,
~/anaconda3/envs/env/lib/python3.6/site-packages/hydra/_internal/utils.py in run_and_report(func)
235 frame = end.tb_frame
236 mdl = inspect.getmodule(frame)
--> 237 assert mdl is not None
238 name = mdl.__name__
239 if name.startswith("omegaconf."):
AssertionError:
AssertionError message at the bottom is empty.
I am not sure what is causing the error.
If I change the magNet
to normal nn.Module
it worked just fine. But I want to use lighning module for some other reasons.
Any kind of help is highly appreciated!
Thank you!!