kmkurn / pytorch-crf

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

To put some constraint on transition matrix, the row index is corresponding to the current state, and the column index is corresponding to the next state? #91

Closed lkqnaruto closed 2 years ago

lkqnaruto commented 2 years ago

Hi,

I'm quite confused about the index for transition matrix. To put some constraint on transition matrix, the row index is corresponding to the current state, and the column index is corresponding to the next state? or the other way?

For example, given label to id: B: 0, I: 1, O: 0, the BIO scheme, if I want to put constraint of O -> I, should I set the transition matrix: self.transition[0, 1] to -1e9? or self.transition[1, 0] to -1e9 ?

I really need you help on this, and I'm quite confused about this.

Thanks!

kmkurn commented 2 years ago

In your label to id mapping, I think you mean to have O: 2 correct? Because index 0 is already used for B. In this case, you want to set self.transition[2, 1] = -1e9. Basically the rows are the current tags, and the columns are the next tags. See how this matrix gets used in e.g., https://github.com/kmkurn/pytorch-crf/blob/e843718540512eb04d9b756fca04a0915affe175/torchcrf/__init__.py#L192.