AttributeError: 'LSTMModel' object has no attribute 'fc'

Hi,

I am trying to train a LSTM Network for a Text Classification task. I have tried instantiating the model through pytorch

model = LSTMModel(input_dim=input_dim, hidden_dim=hidden_dim,layer_dim=layer_dim, output_dim=output_dim)

and through skorch using

classifier = NeuralNetClassifier(
    module=LSTMModel,
    module__input_dim=input_dim,
    module__hidden_dim=hidden_dim,
    module__layer_dim=layer_dim,
    module__output_dim=output_dim,
    batch_size=batch_size,
    max_epochs= num_epochs,
    criterion=nn.CrossEntropyLoss,
    optimizer=torch.optim.Adam,
    lr=0.1,
    train_split=None,
    callbacks=callbacks,
    verbose=1,
    device=device)

in both cases I get AttributeError: ‘LSTMModel’ object has no attribute ‘fc’. The model is defined as follows:

from torch import nn

class LSTMModel(nn.Module):
    def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
        super(LSTMModel, self).__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)
    
    def foward(self, x):
        h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_().to(device)
        c0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_().to(device)
        out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
        out = self.fc(out[:,-1, :])
        return out

input_dim = train_features.shape[2] 
hidden_dim = 32 
layer_dim = 1 
output_dim = len(df.unique())

What am I missing? Pytorch version is torch==1.13.0.

Thank you!

1 Like

You have a typo in your code and are trying to compare self.fc to an nn.Linear layer:

self.fc == nn.Linear(hidden_dim, output_dim)

instead of assigning it:

self.fc = nn.Linear(hidden_dim, output_dim)
2 Likes

Thank you so much! I have been staring at this code for way to long to ever figure this out on my own.

1 Like