Flash
Flash

Reputation: 75

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

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'

Upvotes: 0

Views: 564

Answers (1)

Theodor Peifer
Theodor Peifer

Reputation: 3496

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()

Upvotes: 1

Related Questions