Pytorch: AttributeError: cannot assign module before Module.__init__() call even if initialized
I'm getting the following error:


  AttributeError: cannot assign module before Module.init() call


I'm trying to create an instance of my class :

class ResNetGenerator(nn.Module):
    def __init__(self, input_nc=3, output_nc=3, n_residual_blocks=9, use_dropout=False):
        # super(ResNetGenerator, self).__init__()
        super().__init__()


I'm calling super().__init__() but in vain.

What I am doing wrong here?

Complete Traceback:

File "train.py", line 40, in &lt;module&gt;
    model = ColorizationCycleGAN(args)
File "/path/cycle_gan.py", line 27, in __init__
    self.G_A2B = ResNetGenerator(input_nc=self.input_nc, output_nc=self.output_nc, n_residual_blocks=9, use_dropout=False)
File "/path/.local/lib/python3.6/site packages/torch/nn/modules/module.py", line 544, in __setattr__
    "cannot assign module before Module.__init__() call")
AttributeError: cannot assign module before Module.__init__() call


In fact, I realized that i wasn't calling super().__init__() in the main class ColorizationCycleGAN. Adding this solved the problem.

I hope that this answer will have the effect of reminding you to check to call the super().__init__() function in all classes that inherits from nn.Module.
