Pytorch_lightning module : can't set attribute error

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!!

4 Likes

Damn, I get the same issue. I think they changed the code in a recent version since my target code works fine a week ago. :roll_eyes:

Yes. Now you should use .update method in your class init
I.e. self.hparams.update(hparams)

1 Like

Thank you for the reply. It now returns error "
TypeError Traceback (most recent call last)
in ()
----> 1 model = T5FineTuner(args)

in init(self, hparams)
3 super(T5FineTuner, self).init()
4 # self.hparams = hparams
----> 5 self.hparams.update(hparams)
6
7 self.model = T5ForConditionalGeneration.from_pretrained(hparams.model_name_or_path)

TypeError: ‘Namespace’ object is not iterable".

Is it changing “self.hparams = hparams” into “self.hparams.update(hparams)”?

2 Likes

I followed this link. This should be helpful

3 Likes

Thank you very much, I find “.save_hyperparameters” works for me.

3 Likes

The same error of Namespace’ object is not iterable" I am getting after employing the self.hparams.update(hparams)

it would of great help if you could please write the whole
“.save_hyperparameters” works for me

line of code please

self.save_hyperparameters(params)

1 Like

can you please specify in which file can i find the “self.hparams = hparams” code line? I am getting the same error while running BYOL on jupyter notebook.