2017-03-28 1 views
4

다음 오류가 발생합니다.AttributeError : 모듈 .__ init __() 앞에 모듈을 할당 할 수 없습니다.

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 

다음과 같은 클래스가 있습니다. 나는 다음 줄을 실행할 때

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) 

그래서 :

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

을 나는 위에서 언급 한 오류가 발생합니다. 여기에서 EmbeddingLayerEncoderRNNQuestionClassifier 클래스와 같이 nn.module을 상속받은 클래스입니다.

내가 뭘 잘못하고있어? pytorchsource code for Module 보면

+0

전체 스택 추적을 게시 할 수 있습니까? –

+0

나는 그것을 게시했다. 나는 내가 무엇인가 놓치고있다라고 생각한다. 그러나 무엇이 밖으로 계산할 수 없다!! –

+0

'torch'에 익숙하지 않지만 속성을 수정하기 전에 파생 클래스'__init__'에서'nn.Module .__ init __()'을 호출해야합니다. –

답변

3

, 우리가 Module에서 파생의 문서화 문자열 예에서 볼에는 다음이 포함됩니다

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) 

그래서 당신은 아마 당신의 파생 클래스에서 같은 방법으로 init을 Module의를 호출 할 :

super(QuestionClassifier, self).__init__() 
관련 문제