kmkurn / pytorch-crf

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

issues about crf.decode()? #97

Closed XFR1998 closed 2 years ago

XFR1998 commented 2 years ago

What is the meaning about the 'mask' parameter of the crf.decode()? Could you provide an example about how to use it? When I combine lstm with crf, how can I do this? Thanks~

kmkurn commented 2 years ago

Hi, thanks for using the library.

The mask parameter is to indicate which input tokens are actual words (i.e. not padding/special tokens). So if you have an input of length 3 stored in a tensor of length 5, you should set the mask to be [True, True, True, False, False]. To use the CRF layer on top of an LSTM, let the LSTM compute the emission score tensor of shape BxNxT, and pass this tensor to the CRF as the emissions argument. B=batch size, N=sequence length, T=number of tags.

XFR1998 commented 2 years ago

Thanks for your detailed reply.😄

kmkurn commented 2 years ago

No worries 😄 I assume this is solved so I'm closing the issue.