kmkurn / pytorch-crf

(Linear-chain) Conditional random field in PyTorch.
https://pytorch-crf.readthedocs.io
MIT License
935 stars 151 forks source link

Loss not decreasing! #42

Closed mahao01 closed 2 years ago

mahao01 commented 4 years ago

I use CRF as my model loss like Issue #29, but I found loss didn't decrease! I replace it with BCEWithLogitsLoss and then loss decreases. I have y: (seq_length,) and y_pred: (seq_length, num_classes). Here is my code:

# features is a list here
for epoch in range(epochs):
    for fea in features:
        y_pred = model(fea)
        y_pred= y_pred.reshape(y_pred.shape[0], 1, -1)
        y= y.reshape(y.shape[0], -1)
        loss = -model_gat.crf(y_pred, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

I wonder whether I use the library by mistake? Looking for your help, thanks!

kmkurn commented 4 years ago

Hi, your code looks fine to me so I'm not sure why the loss didn't decrease. I've confirmed with a simple code that the CRF works fine. Could you post the smallest working code to reproduce the behaviour?

mahao01 commented 4 years ago

Thanks very much, I'll check my code again.