0

While making predictions from my LSTM model, I am getting the error :: AttributeError: 'LSTMClassifier' object has no attribute 'log_softmax'.Can anyone explain me what I am doing wrong?

class LSTMClassifier(nn.Module):

    def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
        super().__init__()
        self.hidden_dim = hidden_dim
        self.layer_dim = layer_dim
        self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, batch_first=True)
        self.fc = nn.Linear(hidden_dim, output_dim)
        self.batch_size = None
        self.hidden = None
      

    def forward(self, x):
        h0, c0 = self.init_hidden(x)
        out, (hn, cn) = self.lstm(x, (h0, c0))
        out = self.fc(out[:, -1, :])
        return out

    def init_hidden(self, x):
        h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim)
        c0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim)
        print(x.size(0))
        print(layer_dim)
        return [t.to(device) for t in (h0, c0)] 
test_dl = DataLoader(tst_data, batch_size=64, shuffle=False)
test = []
print('Predicting on test dataset')
for batch, _ in tst_data:
    batch=batch.to(device)
    print(batch.shape)
    out = model.to(device)
    y_hat = F.log_softmax(out, dim=1).argmax(dim=1) ### at this line I am getting error
    test += y_hat.tolist()

Thank you in advance!

Error :: AttributeError: 'LSTMClassifier' object has no attribute 'log_softmax'

Traceback:::

AttributeError                            Traceback (most recent call last)
<ipython-input-74-df6f970f9b87> in <module>()
      8     print(batch.shape)
      9     out = model.to(device)
---> 10     y_hat = F.log_softmax(out, dim=1).argmax(dim=1)
     11 
     12     test += y_hat.tolist()

1 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
   1129                 return modules[name]
   1130         raise AttributeError("'{}' object has no attribute '{}'".format(
-> 1131             type(self).__name__, name))
   1132 
   1133     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

AttributeError: 'LSTMClassifier' object has no attribute 'log_softmax'

2
  • Did you check the value of F and made sure it was torch.nn.functional? Could you provide the full error trace? Commented Jul 23, 2021 at 21:44
  • I have added the error trace in the question itself. Commented Jul 24, 2021 at 6:48

1 Answer 1

1

Your train loop does not work. You never pass the input batch to the model therefore out is not a output tensor but a model object which of course can't be passed into an activation function. You have to do this:

model = model.to(device)
for batch, _ in tst_data:
    batch = batch.to(device)

    # pass your input batch to the model like this
    out = model.train()(batch)
    
    # now you can calculate the log-softmax for out
    y_hat = F.log_softmax(out, dim=1).argmax(dim=1)
    test += y_hat.tolist()
Sign up to request clarification or add additional context in comments.

1 Comment

I got another issue after adding this line : RuntimeError: input must have 3 dimensions, got 1. It is beacuse batch having size 4000 and the size should be of ([1, 4000, 500]). But I dont know how to rectify this .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.