Your code works fine using dummy inputs and targets:
class LSTMClassifier(nn.Module):
    def __init__(self):
        super(LSTMClassifier, self).__init__()
        self.lstm = nn.LSTM(1, 64, num_layers=1,batch_first =True )
        self.hidden2out = nn.Linear(64,2)
        self.dropout_layer = nn.Dropout(p=0.3)
    def forward(self, data):
        outputs, (ht, ct) = self.lstm(data, None)
        output = self.dropout_layer(ht[-1])
        output = self.hidden2out(output)
        return output

model = LSTMClassifier()

x = torch.randn(1, 1, 1)
target = torch.randint(0, 2, (1,))
criterion = nn.CrossEntropyLoss()

output = model(x)
loss = criterion(output, target)
loss.backward()
print(model.lstm.weight_ih_l0.grad.abs().sum())
> tensor(0.5159)

There are some minor issues in your code, which should be unrelated or would throw an error in newer PyTorch versions:


Variables are deprecated since 0.4, so you can use tensors directly
setting the requires_grad attribute on non-leaf variables should throw an error, so remove output.requires_grad = True and loss.requires_grad = True