I solve problem of classification. I have a lot of keywords in text and must detected their existence in text. I used torchtext classification with embeddingbag and embedding. Accuracy was equal to 90%. But, I hope that model prediction will be 95% +.
Now, I use embedding + LSTM and have global problem with loss. Loss is not change. I was changing lr from 1000 to 0.0001, but loss change from 0.7 to 0.69.
Help me please
class TextClassificationModel_vec(nn.Module):
def __init__(self, vocab_size, embed_dim, hidden_size):
super(TextClassificationModel_vec, self).__init__()
self.embedding = nn.Embedding(vocab_size, embed_dim, padding_idx=0, sparse=True)
self.lstm = nn.LSTM(input_size=embed_dim, hidden_size=hidden_size, num_layers=1, batch_first=True)
self.fc = nn.Linear(hidden_size, 1)
self.dropout = nn.Dropout(p=0.6)
def forward(self, x):
x = self.embedding(x)
x = self.dropout(x)
x, __ = self.lstm(x)
x = self.fc(x[:, -1, :])
return x
Have you tried printing out the gradient values for model.parameters() after calling loss.backward()? Maybe your initial gradient is near 0, so the model won’t update at all?
I was reading through this tutorial on Text Classification and I can’t really see what’s wrong with your model.
One thing that could be a problem is with the cost function BCEWithLogitsLoss , after looking at the docs they pass a pos_weight variable. (which defaults to None). Perhaps this could be the issue? Maybe you’re multipling all your losses by None which would give a near 0 gradient?
Then I don’t know what else to recommend. Perhaps there’s something of use in the tutorial I shared above? Perhaps someone else with more experience might be able to help! Sorry!
I have change LSTM on GRU and it work. But, accuracy is lower than when I used only Embedding.
Embedding + Linear: acc = 0.95
Embedding + GRU + Linear: acc = 0.9
That’s pretty strange behaviour, perhaps mention this to a developer? They might have a better idea as to why it works with a GRU rather than an LSTM! But glad to hear it’s working!