tf_ner icon indicating copy to clipboard operation
tf_ner copied to clipboard

Label probabilities for the CRF layer

Open ashim95 opened this issue 5 years ago • 4 comments

Hi, Thanks for sharing this great implementation. I know it is possible to get the label probabilities using forward backward algorithm in CRFs. I am finding some difficulties in implementing/modifying the default CRF implementation in tensorflow. For calculation of the partition function, they have only used the forward (message passing) algorithm. Do you have any experience or ideas about how the forward-backward algorithm could be implemented in tf?

ashim95 avatar Feb 18 '19 14:02 ashim95

Hi @ashim95, This is a very good point, to compute the marginals (per token tag probability) you would indeed need to modify the tensorflow CRF implementation and use the Foward / Backward algorithm. I did not do it, so I cannot give you estimate of the complexity of the task, but it seems like the Forward cell is already implemented in tensorflow.

For reference, the crfsuite uses the F/B approach to compute the marginals it seems https://github.com/chokkan/crfsuite/blob/dc5b6c7b726de90ca63cbf269e6476e18f1dd0d9/lib/crf/src/crf1d_context.c

If you're up to it, we could collaborate and open a PR in Tensorflow adding this support.

Let me know.

guillaumegenthial avatar Apr 12 '19 05:04 guillaumegenthial

I have requested this feature on issues page of tensorflow. https://github.com/tensorflow/tensorflow/issues/42178 Please comment there and try to upvote it so that they can add this functionality.

agarwalishan avatar Aug 10 '20 06:08 agarwalishan

Hi @agarwalishan I would suggest you look into pytorch based implementations of CRF. Many of these output marginal probabilities. Even if you can not find such an implementation on github, it should be much easier to implement in pytorch than tensorflow.

ashim95 avatar Aug 10 '20 08:08 ashim95

Thanks for the reply but I want tensorflow implementation only.

agarwalishan avatar Aug 10 '20 08:08 agarwalishan