[solved] AttributeError: type object 'object' has no attribute '__getattr__'

I have a seq2seq network (a class) which is trained and model states are saved without any problem. [Please note, I am using DataParallel]

Constructor of that class.

class Sequence2Sequence(nn.Module):
    """Class that classifies question pair as duplicate or not."""

    def __init__(self, dictionary, embedding_index, max_sent_length, args):
        """"Constructor of the class."""
        super(Sequence2Sequence, self).__init__()
        self.dictionary = dictionary
        self.embedding_index = embedding_index
        self.config = args
        self.encoder = Encoder(len(self.dictionary), self.config)
        self.decoder = AttentionDecoder(len(self.dictionary), max_sent_length, self.config)
        self.criterion = nn.NLLLoss()  # Negative log-likelihood loss

        # Initializing the weight parameters for the embedding layer in the encoder.
        self.encoder.init_embedding_weights(self.dictionary, self.embedding_index, self.config.emsize)

Now for testing, I wrote the following in my test function.

def test(model, batch_sentence):
    if model.config.model == 'LSTM':
        encoder_hidden, encoder_cell = model.encoder.init_weights(batch_sentence.size(0))
        output, hidden = model.encoder(batch_sentence, (encoder_hidden, encoder_cell))
        encoder_hidden = model.encoder.init_weights(batch_sentence.size(0))
        output, hidden = model.encoder(batch_sentence, encoder_hidden)

In the first line of the test function, I am getting the following error.

AttributeError: type object 'object' has no attribute '__getattr__'

Any idea why it is not working?

I solved the problem. When I use DataParallel, the object belong to DataParallel instead of my custom class and as a result I was having the issue.


So how did you extract it out of the DataParallel class before saving it?

I saved the model with DataParallel. After loading, I used the model as module. If you print the model after loading using DataParallel, you will see the model is inside the DataParallel which acts like a wrapper. You can access the model using DataParallel.model. Moreover, you can also load the model without DataParallel even though you trained and saved the model with DataParallel.