tensor2tensor icon indicating copy to clipboard operation
tensor2tensor copied to clipboard

Adding Regularizer in Cross Entropy Loss for Repeated Words

Open NickShahML opened this issue 9 years ago • 10 comments

Hey T2T team,

Great repo. I've been trying to investigate how to add a penalty for repeated words which occur sometimes in translations. For example "He went to get milk milk milk".

Inspired by coverage, I think the best way to do this is to add the following regulization term:

# For all timesteps, we add the softmaxed logits. This tells the total probability of particular words guessed.
# Assume `logits` is [batch, timesteps, vocab_size]
logits_summed_across_timesteps = tf.reduce_sum(
    [tf.nn.softmax(logits) for logits in all_timesteps], axis=1)

# If there are any words guessed more than once (greater probability than 1.0), then we add this to the loss
regularization = tf.reduce_sum(
    tf.maximum(1.0, logits_summed_across_timesteps)  - 1.0))

How can this be done for the transformer? In the padded_cross_entropy function, it seems that you can only see one timestep. So I'm not sure how you can implement this for all timesteps. Also, this would require the softmaxed logits. Since softmax_cross_entropy_with_logits does the softmax internally in tensorflow, you may have to compute the softmax a separate time?

Any help with this would be greatly appreciated!

NickShahML avatar Oct 18 '17 20:10 NickShahML

@NickShahML I'm working on a similar bit. Not sure why the model is repeating itself so much. In any case I think the way you want to go about it is to operate on the logits, not the output of the softmax. The general idea that Ive implemented is for the output logit at each time step, compare to all outputs in and previous timesteps (essentially the same operation as self-attention). The comparison here will be the cosine simularity between the logits. This will be high when the model is repeating itself and low otherwise. Cosine is between [-1,1]. Ill make the simplifying assumption that we can just take the maximum reduction over time of these cosine values as our loss. You could also do a mean reduction or a sum reduction. So now we have a single cosine similarity score for each timestep (which is the maximum similarity of that output with all previous outputs). We will threshold those values so everything below some value (say 0.8) gets masked to 0. Now we have a max cosine score if its above 0.8 and 0 else. We can incorporate these scores into the loss function by multiplying them by some hyperparameter (scaling) and then adding to the regular loss for each timestep. The only additional thing I think is important is controlling the gradient flow for the similarity loss so that it only propagates to the output that is farther forward in time. That way we dont penalize the model for outputting something that gets repeated later, only for repeating something that has already been output. Not sure where you would put this loss in the t2t framework as I use it mostly as a library and pull out the various pieces i need

lucasgreene avatar Nov 07 '17 00:11 lucasgreene

Here is a blog explaining a similar but slightly different approach http://www.abigailsee.com/2017/04/16/taming-rnns-for-better-summarization.html . They keep track of the produced attention states and penalize the model for attending to the same thing multiple times. I feel like penalizing the model for producing similar outputs is more general, as the model could repeat itself without using attention. In any case they report success with this approach and I have yet to make the cosine loss reduce the repetition.

lucasgreene avatar Nov 08 '17 17:11 lucasgreene

Thanks @lucasgreene. I talked to Abigail a while back and I definitely their coverage approach is promising. However, with multiple attention heads, I'm not totally sure how this technique can be applied. Applying this to each head would occur a huge amount of memory usage, and performance would probably drop.

This is why I advocated penalizing by adding an extra penalty for words that have been already used in the softmax approach. I actually think this is the perferred method given what has been happening with MCTS in alpha go zero.

NickShahML avatar Nov 08 '17 20:11 NickShahML

If you implement your own Modality, you can override the loss function.

rsepassi avatar Nov 14 '17 00:11 rsepassi

@rsepassi how do you use an implemented Modality using t2t-trainer? I understand how to register them, as in:

@registry.register_symbol_modality("foo")
class FooSymbolModality(SymbolModality):
  """SymbolModality that uses Foo loss."""

  def loss(self, logits, targets):
    # ...

But not sure how to tap into the registry

wenkesj avatar Jan 19 '18 21:01 wenkesj

@wenkesj: I think you are looking for --t2t_usr_dir, see https://github.com/tensorflow/tensor2tensor#adding-your-own-components

martinpopel avatar Jan 19 '18 21:01 martinpopel

@martinpopel Yes, I understand how to register them. I want to know how to target the use of the specific modality on the problem/model, i.e. replacing the CTCSymbolModality with FooSymbolModality

wenkesj avatar Jan 19 '18 22:01 wenkesj

Oh, sorry, I did not understand. I have never played with the modalities, but checking the code I see https://github.com/tensorflow/tensor2tensor/blob/a1d7ed7e7d96ea0d89ca153ba46b7f51d394580f/tensor2tensor/layers/common_hparams.py#L136-L143

martinpopel avatar Jan 19 '18 22:01 martinpopel

@martinpopel that would be it!

i.e. for those visiting this:

  1. Follow the tutorial on adding your own components with --t2t_usr_dir

  2. Within the component module, implement your loss function as a modality, i.e. for symols under the name "symbol:foo":

@registry.register_symbol_modality("foo")
class FooSymbolModality(SymbolModality):
  """SymbolModality that uses Foo loss."""

  def loss(self, logits, targets):
    # ...
  1. Update the input_modalities and target_modality fields in your component hparams, as in https://github.com/tensorflow/tensor2tensor/blob/a1d7ed7e7d96ea0d89ca153ba46b7f51d394580f/tensor2tensor/layers/common_hparams.py#L136-L143 And change them too "symbol:foo". This will probably work.

wenkesj avatar Jan 19 '18 22:01 wenkesj

@NickShahML Have you ever added the penalty for repeated works? Does that work? I'm facing the same problem now. Thx.

Chris19920210 avatar Nov 12 '18 07:11 Chris19920210