Pytorch_lightning module : can't set attribute error

username: Ajinkya_Ambatwar
cooked: <p>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 <code>self.hparams</code>, it returns an attribute error</p>
<p><code>AttributeError: can't set attribute</code></p>
<p>I am following the structure from the official pytorch-lightning docs. But I am not sure, why is this error coming.<br>
Here’s my pytorch Lightning module definition</p>
<pre><code class="lang-auto">class magNet(LightningModule):
    def __init__(self, hparams, *args, **kwargs):
        super().__init__()
        self.hparams = hparams
        print(self.hparams)
        self._build_model()

   def _build_model(self):
        &lt;defining architecture&gt;
   def forward(self):
        &lt;forward pass&gt;
   def training_step(self, batch, batch_idx):
        &lt;training step&gt;
</code></pre>
<p>In my <code>main()</code></p>
<p>This function converts all yaml files in the config folder into a single hparams dictionary.</p>
<pre><code class="lang-auto">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()
</code></pre>
<p>Returns</p>
<pre><code class="lang-auto">ipython-input-3-48509a4d671d&gt; in main(cfg)
      4      print(params)
----&gt; 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__()
--&gt; 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:
--&gt; 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)
&lt;ipython-input-4-263240bbee7e&gt; in &lt;module&gt;
----&gt; 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,
---&gt; 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(
--&gt; 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)
--&gt; 237                     assert mdl is not None
    238                     name = mdl.__name__
    239                     if name.startswith("omegaconf."):

AssertionError:
</code></pre>
<p>AssertionError message at the bottom is empty.</p>
<p>I am not sure what is causing the error.</p>
<p>If I change the <code>magNet</code> to normal <code>nn.Module</code> it worked just fine. But I want to use lighning module for some other reasons.</p>
<p>Any kind of help is highly appreciated!</p>
<p>Thank you!!</p>

username: Binge_Kenny
cooked: <p>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. <img src="https://discuss.pytorch.org/images/emoji/apple/roll_eyes.png?v=9" title=":roll_eyes:" class="emoji" alt=":roll_eyes:"></p>

username: Ajinkya_Ambatwar
cooked: <p>Yes. Now you should use <code>.update</code> method in your class init<br>
I.e. <code>self.hparams.update(hparams)</code></p>

username: Binge_Kenny
cooked: <p>Thank you for the reply. It now returns error "<br>
TypeError                                 Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 model = T5FineTuner(args)</p>
<p> in <strong>init</strong>(self, hparams)<br>
3     super(T5FineTuner, self).<strong>init</strong>()<br>
4     # self.hparams = hparams<br>
----&gt; 5     self.hparams.update(hparams)<br>
6<br>
7     self.model = T5ForConditionalGeneration.from_pretrained(hparams.model_name_or_path)</p>
<p>TypeError: ‘Namespace’ object is not iterable".</p>
<p>Is it changing “self.hparams = hparams” into “self.hparams.update(hparams)”?</p>

username: Ajinkya_Ambatwar
cooked: <p>I followed this link. This should be helpful</p>
<aside class="onebox githubfolder">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/PyTorchLightning/pytorch-lightning/discussions/7525" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <img src="https://github.com/opengraph.png" class="thumbnail onebox-full-image" width="60" height="60">

<h3><a href="https://github.com/PyTorchLightning/pytorch-lightning/discussions/7525" target="_blank" rel="noopener nofollow ugc">Pytorch_lightning module : can't set attribute error · Discussion #7525 ·...</a></h3>

<p><a href="https://github.com/PyTorchLightning/pytorch-lightning/discussions/7525" target="_blank" rel="noopener nofollow ugc">//github.com/PyTorchLightning/pytorch-lightning/discussions/7525</a></p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: Binge_Kenny
cooked: <p>Thank you very much, I find “.save_hyperparameters” works for me.</p>

username: Aditya_Srivastava
cooked: <p>The same error of Namespace’ object is not iterable" I am getting after employing the self.hparams.update(hparams)</p>
<p>it would of great help if you could please write the whole<br>
“.save_hyperparameters” works for me</p>
<p>line of code please</p>

username: chen-yifu
cooked: <p>self.save_hyperparameters(params)</p>

username: Rajdeep_Das1
cooked: <p>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.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/a/8a26d5c9235ec3262fe3060c427ce27fbf269ffd.png" data-download-href="https://discuss.pytorch.org/uploads/default/8a26d5c9235ec3262fe3060c427ce27fbf269ffd" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/a/8a26d5c9235ec3262fe3060c427ce27fbf269ffd.png" alt="image" data-base62-sha1="jI95SbnKG2rScSIbCjJ8hvvHxuZ" width="690" height="415" data-dominant-color="F8F9F8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1089×656 38 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>