Body: <p>We're working on a sequence-to-sequence problem using pytorch, and are using cross-entropy to calculate the loss when comparing the output sequence to the target sequence. This works fine and penalizes the model correctly. However, we also have the added constraint that the prediction can't contain repeated indices, e.g.</p>
  <p><code>good: [1, 2, 3, 4, 5]</code></p>
  <p><code>bad: [1, 2, 2, 4, 5]</code></p>
  <p>We would like to add an additional penalty term that <em>punishes</em> the model further for producing duplicate indices, which would be added to the cross-entropy loss.</p>
  <p>How would I construct this additional loss function in pytorch?</p>
  <p>PS: Yes it's true that we could just <em>hack</em> the code-generation piece to not generate duplicate indices and then incorporate this into our beam-search, but I would like to first see whether this additional constraint produces a better model!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-06-02T01:24:13.893Z
Comments.Text: This is a bit confusing - do you want a loss that acts on each output individually or all at once? Or just adjacent ones? [I've created a diagram here of my understanding of what you want](https://imgur.com/a/jR71oP2)
Comments.UserDisplayName: Recessive
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-06-02T11:42:42.540Z
Comments.Text: The indices from the sequence correspond to token positions in the embedding, so we need a loss function that is 0 when all indices are unique, and then (say) 1 when there is 1 duplicate index generated by the model (as in the "bad" case above).
Comments.UserDisplayName: vgoklani
Tags: <objective-functions><optimization><pytorch><constraint-satisfaction-problems><constrained-optimization>
Title: How to create a loss function that penalizes duplicate indices in the output tensor?
UserDisplayName: vgoklani
ThreadText: # How to create a loss function that penalizes duplicate indices in the output tensor?
  
  (Asked by: username_0 on 2022-06-01)
  
  We're working on a sequence-to-sequence problem using pytorch, and are using cross-entropy to calculate the loss when comparing the output sequence to the target sequence. This works fine and penalizes the model correctly. However, we also have the added constraint that the prediction can't contain repeated indices, e.g.
  
  `good: [1, 2, 3, 4, 5]`
  
  `bad: [1, 2, 2, 4, 5]`
  
  We would like to add an additional penalty term that *punishes* the model further for producing duplicate indices, which would be added to the cross-entropy loss.
  
  How would I construct this additional loss function in pytorch?
  
  PS: Yes it's true that we could just *hack* the code-generation piece to not generate duplicate indices and then incorporate this into our beam-search, but I would like to first see whether this additional constraint produces a better model!
  
  **Tags:** objective-functions, optimization, pytorch, constraint-satisfaction-problems, constrained-optimization
  > **Comments:**
  > **username_1**: This is a bit confusing - do you want a loss that acts on each output individually or all at once? Or just adjacent ones? [I've created a diagram here of my understanding of what you want](https://imgur.com/a/jR71oP2)  
  > **username_0**: The indices from the sequence correspond to token positions in the embedding, so we need a loss function that is 0 when all indices are unique, and then (say) 1 when there is 1 duplicate index generated by the model (as in the "bad" case above).  
  
  ---
  
  ## 0 answers
  
  