Decoding and sampling · Medium

Contrastive Search Reranking

Problem 26 of 50 · Python and NumPy

Problem

Select a candidate index using contrastive score = (1−alpha)*probability − alpha*degeneration. Degeneration is the maximum cosine similarity between that candidate embedding and any history embedding.

Use zero similarity if either vector has zero norm; if history is empty, degeneration is zero. Negative cosine similarities remain negative. Return the smallest candidate index among equal scores; candidate indices are the token IDs for this exercise.

def contrastive_rerank(probabilities, candidates, history, alpha):

Constraints

  • 1 ≤ candidate count,D ≤ 16; candidates is C × D, probabilities has C values in [0,1].
  • history is H × D or [], 0 ≤ H ≤ 16; 0 ≤ alpha ≤ 1; vectors are finite.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    0.2,
    0.8
  ],
  [
    [
      1,
      0
    ],
    [
      0,
      1
    ]
  ],
  [
    [
      1,
      0
    ]
  ],
  0
]
Expected result: 1

alpha=0 removes the similarity penalty, so candidate 1 wins on probability 0.8.

Example 2

Arguments: [
  [
    0.6,
    0.4
  ],
  [
    [
      1,
      0
    ],
    [
      0,
      1
    ]
  ],
  [
    [
      1,
      0
    ]
  ],
  0.5
]
Expected result: 1

Candidate 0 repeats history exactly and is penalized; orthogonal candidate 1 wins.

Sign in to solve this problem

Browse all coding problems · Learn the foundations