kmkurn / pytorch-crf

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

so has anyone got this pytorch crf to work yet ? #50

Closed yuhujia closed 4 years ago

yuhujia commented 4 years ago

Looks like this implementation does not work on real data. If anyone has gotten it to work (aka has good performance and (optional) deployed to production) pls correct me here.

kmkurn commented 4 years ago

I have used this myself for production (when I was still working for my former employer). Sorry it doesn't work for you.

yuhujia commented 4 years ago

great ! can you post scripts/data/steps to reproduce your performance ?

kmkurn commented 4 years ago

Unfortunately no, because it was all proprietary. But it was basically an implementation of this paper.

yuhujia commented 4 years ago

Steps to reproduce numbers mentioned in the paper would be great.

kmkurn commented 4 years ago

I'm not sure what steps you need other than those described in the paper.

robinsongh381 commented 4 years ago

I have made good products with REAL data :smile:

elsheikh21 commented 4 years ago

@robinsongh381 @kmkurn @yuhujia anyone got it to work or not yet?

kmkurn commented 4 years ago

@elsheikh21 Have you tried it yourself? I personally have used this, and it worked.

elsheikh21 commented 4 years ago

I personally tried to use it, however, I could not. I sent you an email

yuye2133 commented 4 years ago

There is a problem with the latest version of pytorch(1.4), log-likehihood will overflow.

elsheikh21 commented 4 years ago

To all future readers, I would like to start by thanking @kmkurn for helping me out, and here is how to utilize his module, I hope it makes everything clear for you (future reader)

So, how to make this work? Firstly, install the module pip install pytorch-crf Secondly, in your script from torchcrf import CRF

Thirdly, add the CRF layer after the model's final layer, self.crf = CRF(num_classes, batch_first=True) where number of tags/output calsses

Forth, in the training loop

for epoch in range(epochs):
   self.model.train()
   for batch_idx, samples in enumerate(train_dataset):
      loss = -self.model.log_probs(inputs, labels)
      sample_loss.backward()
      self.optimizer.step()
   # Compute val_loss

But what is model.log_probs(inputs, labels)?

    """
     Build a simple BiLSTM 
    """
    def forward(self, x):
        ''' Leave model forward function untouched '''
        embeddings = self.word_embedding(x)  # [Samples_Num, Seq_Len]
        embeddings = self.dropout(embeddings)
        o, _ = self.lstm(embeddings)        # output shape: [Samples_Num, Seq_Len, Tags_Num]
        o = self.dropout(o)
        logits = self.classifier(o)
        return logits

    def log_probs(self, x, tags, mask=None):
        # mask = (x != 0).float()  # assuming that padding index is 0
        emissions = self(x)  # Compute Emission scores
        return self.crf(emissions, tags, mask=mask)

Lastly, how to predict outputs as per model? predictions = self.model.predict(inputs) # returns predicted classes, no need to invoke argmax(..)

what is model.predict(inputs)?

    def predict(self, x):
        emissions = self(x)
        return self.crf.decode(emissions)

I was using Python3.6.4 and PyTorch 1,4,0

kmkurn commented 4 years ago

The library seems to work just fine, so I'm closing this issue.