Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

624
Visualizações
AttributeError: cannot assign module before Module.__init__() call

I am getting the following error.

Traceback (most recent call last):
  File "main.py", line 63, in <module>
    question_classifier = QuestionClassifier(corpus.dictionary, embeddings_index, corpus.max_sent_length, args)
  File "/net/if5/wua4nw/wasi/academic/research_with_prof_chang/projects/question_answering/duplicate_question_detection/source/question_classifier.py", line 26, in __init__
    self.embedding = EmbeddingLayer(len(dictionary), args.emsize, args.dropout)
  File "/if5/wua4nw/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py", line 255, in __setattr__
    "cannot assign module before Module.__init__() call")
AttributeError: cannot assign module before Module.__init__() call

I have a class as follows.

class QuestionClassifier(nn.Module):

     def __init__(self, dictionary, embeddings_index, max_seq_length, args):
         self.embedding = EmbeddingLayer(len(dictionary), args.emsize, args.dropout)
         self.encoder = EncoderRNN(args.emsize, args.nhid, args.model, args.bidirection, args.nlayers, args.dropout)
         self.drop = nn.Dropout(args.dropout)

So, when I run the following line:

question_classifier = QuestionClassifier(corpus.dictionary, embeddings_index, corpus.max_sent_length, args)

I get the above mentioned error. Here, EmbeddingLayer and EncoderRNN is a class written by me which inherits nn.module like the QuestionClassifier class.

What I am doing wrong here?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Looking at the pytorch source code for Module, we see in the docstring an example of deriving from Module includes:

 class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.conv1 = nn.Conv2d(1, 20, 5)
            self.conv2 = nn.Conv2d(20, 20, 5)

So you probably want to call Module's init the same way in your derived class:

super(QuestionClassifier, self).__init__()
over 4 years ago · Santiago Trujillo Relatório

0

Pytorch keeps track of the submodules(conv1, conv2) you will write in your custom Module. Under the hood, the graph corresponding to your Model is automatically built.

The nested Modules will be added to an OrderedDict _modules (initialized in nn.Module.__init__) See source(L69)

If nn.Module.__init__ is not called (self._modules would equal to None), when trying to add a Module, it will raise an error (no key can be added to None). See source(L540-544)

Inspired from the doc:

 class CustomModule(nn.Module):
        def __init__(self):
            super(CustomModule, self).__init__() # Initialize self._modules as OrderedDict
            self.conv1 = nn.Conv2d(1, 20, 5)     # Add key conv1 to self._modules
            self.conv2 = nn.Conv2d(20, 20, 5)    # Add key conv2 to self._modules 
over 4 years ago · Santiago Trujillo Relatório

0

This usually happens when super class's init has not been called. In this case one should start their Neural network class with super.__init__() call. The code would look like this:

class QuestionClassifier(nn.Module):
    def __init__(self, dictionary, embeddings_index, max_seq_length, args):
       super().__init__()

This super's init call should be within this class's init code.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda