addons icon indicating copy to clipboard operation
addons copied to clipboard

crf_losses.py as in keras_contrib?

Open ahmad-alismail opened this issue 3 years ago • 8 comments

Hello, I want to ask if you there is an alternative crf_losses.py as in keras_contrib in tensorflow_addons?

ahmad-alismail avatar Jan 23 '21 06:01 ahmad-alismail

Hi @afi1289,

I believe that we have a crf layer however we do not have an alternative for crf losses.

Harsh188 avatar Jan 23 '21 07:01 Harsh188

Thank you for your reply @Harsh188 Could you add crf_losses.py to losses, crf_accuracies.py to metrics? thats will help a lot (especially for beginners like me) by implementing BiLSTM-CRF models which initially implemented using keras-contrib. Thank you in advance!

ahmad-alismail avatar Jan 23 '21 20:01 ahmad-alismail

Hi @Harsh188 , I agree with @afi1289 , please add them

kankajan814 avatar Jan 23 '21 20:01 kankajan814

You can check https://github.com/xuxingya/tf2crf

I don't know if @xuxingya Is interested to contribute here.

bhack avatar Jan 23 '21 21:01 bhack

If you won't mind, I'd like to take a stab at this. I've found a repo which implements CRF Loss.

CRF Loss: https://github.com/howl-anderson/tf_crf_layer/blob/master/tf_crf_layer/loss/crf_loss.py CRF Accuracy: https://github.com/howl-anderson/tf_crf_layer/blob/master/tf_crf_layer/metrics/crf_accuracy.py

A bit of polish, and should make for a good PR, I suppose?

EDIT: More resources: https://github.com/mtreviso/linear-chain-crf

jonpsy avatar Jan 24 '21 17:01 jonpsy

Okay, I did some research on this. Thing is, the loss function doesn't solely depend on y_true and y_pred but also the internal variables. So, we can't use eager tensor for y_pred(since keras.contrib uses _keras_history to access internal variables, more at this issue: https://github.com/tensorflow/tensorflow/issues/37119

@afi1289 Further investigations revealed that, you could add a custom wrapper around our CRF class for loss as is shown here, sample tutorial here.

jonpsy avatar Jan 27 '21 16:01 jonpsy

@afi1289 , can this be closed as part of #1999 ?

ashutosh1919 avatar May 13 '21 11:05 ashutosh1919

If you won't mind, I'd like to take a stab at this. I've found a repo which implements CRF Loss.

CRF Loss: https://github.com/howl-anderson/tf_crf_layer/blob/master/tf_crf_layer/loss/crf_loss.py CRF Accuracy: https://github.com/howl-anderson/tf_crf_layer/blob/master/tf_crf_layer/metrics/crf_accuracy.py

A bit of polish, and should make for a good PR, I suppose?

I am the author of this repo, and I happy to bring back the CRF accuracy feature to TFA. I will submit a PR for it when it is ready.

constraints in the AllenNLP (https://docs.allennlp.org/main/api/modules/conditional_random_field/) is awesome feature too. It also exits in my repo (in a different branch). I hope I can bring it to the TFA in the future.

howl-anderson avatar Aug 28 '21 16:08 howl-anderson

TensorFlow Addons is transitioning to a minimal maintenance and release mode. New features will not be added to this repository. For more information, please see our public messaging on this decision: TensorFlow Addons Wind Down

Please consider sending feature requests / contributions to other repositories in the TF community with a similar charters to TFA: Keras Keras-CV Keras-NLP

seanpmorgan avatar Mar 01 '23 03:03 seanpmorgan