Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add class weights for optimizing the loss of tfa.text.crf model #817

Closed
snailcoder opened this issue Dec 28, 2019 · 6 comments
Closed

Add class weights for optimizing the loss of tfa.text.crf model #817

snailcoder opened this issue Dec 28, 2019 · 6 comments

Comments

@snailcoder
Copy link

Describe the feature and the current behavior/state.
It is necessary to add class weights when optimizing the loss of classification model on imbalance data. For example, when use tf.nn.softmax_cross_entropy_with_logits to calculate the model's loss, we can obtain a cross entropy tensor of which the shape is the same as labels so we can multiply class weights with the cross entropy to get a weighted loss. Similarly, when use tfa.text.crf, we should also get the weighted loss.

Relevant information

  • Are you willing to contribute it (yes/no): no
  • Are you willing to maintain it going forward? (yes/no): no
  • Is there a relevant academic paper? (if so, where): no
  • Is there already an implementation in another framework? (if so, where): no
  • Was it part of tf.contrib? (if so, where): no

Which API type would this fall under (layer, metric, optimizer, etc.)
metric
Who will benefit with this feature?
All tfa.text.crf users
Any other info.

@Squadrick
Copy link
Member

Hello, tfa.text.crf is only for base-level ops. We have a PR for adding CRF layer in #377

Once we have the basic version of CRF layer merged, we'll get to adding in class weights to the loss.

@dhakrasp
Copy link

Can anyone guide me as to how I could go about modifying the current implementation of CRF loss so as to include class weights?

@bhack
Copy link
Contributor

bhack commented Jul 20, 2020

/cc @jaspersjsun

@jaspersjsun
Copy link
Contributor

@bhack Thanks for cc me. I'm not sure whether it is necessary to add it to the official losses. Do we have further papers, materials or discussions to make this more convincing?

Since only changing the emission scores (tfa.text.crf.crf_unary_score) could be problematic, Gael de Lannoy et al. proposed an implementation for weighted CRF. Maybe try to follow it and create a custom loss function with existing ops in tfa.text.crf could be an option.

@eraldoluis
Copy link

eraldoluis commented Oct 7, 2020

@jaspersjsun when you say that changing the emission scores could be problematic, you mean theoretically or in terms of the code base?

I'm studying this issue with the intent of implementing some approach (first in AllenNLP, then I could do it in TF as well). I think Lannoy et al.'s approach is theoretically flawed. I mean, I believe it can work in practice, but their theoretical motivation is not correct. If this is the case, we could do it with a much simpler approach (like weighted emission scores).

@seanpmorgan
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants