so has anyone got this pytorch crf to work yet ?
yuhujia opened this issue ยท 12 comments
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.
I have used this myself for production (when I was still working for my former employer). Sorry it doesn't work for you.
great ! can you post scripts/data/steps to reproduce your performance ?
Unfortunately no, because it was all proprietary. But it was basically an implementation of this paper.
Steps to reproduce numbers mentioned in the paper would be great.
I'm not sure what steps you need other than those described in the paper.
I have made good products with REAL data ๐
@robinsongh381 @kmkurn @yuhujia
anyone got it to work or not yet?
@elsheikh21 Have you tried it yourself? I personally have used this, and it worked.
I personally tried to use it, however, I could not. I sent you an email
There is a problem with the latest version of pytorch(1.4), log-likehihood will overflow.
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
The library seems to work just fine, so I'm closing this issue.