對比採樣器

[原始碼]

ContrastiveSampler 類別

keras_hub.samplers.ContrastiveSampler(k=5, alpha=0.6, **kwargs)

對比採樣器類別。

這個採樣器實作了對比搜尋演算法。簡而言之,此採樣器選擇具有最高「分數」的 token 作為下一個 token。「分數」是 token 的機率與先前 tokens 的最大相似度之間的加權總和。透過使用這個聯合分數,對比採樣器減少了重複已見 tokens 的行為。

參數

  • k:整數 (int),top-k 的 k 值。下一個 token 將從 k 個 tokens 中選擇。
  • alpha:浮點數 (float),聯合分數計算中負最大相似度的權重。alpha 值越大,分數就越依賴相似度而非 token 機率。

呼叫參數

{{call_args}}

範例

causal_lm = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en")

# Pass by name to compile.
causal_lm.compile(sampler="contrastive")
causal_lm.generate(["Keras is a"])

# Pass by object to compile.
sampler = keras_hub.samplers.ContrastiveSampler(k=5)
causal_lm.compile(sampler=sampler)
causal_lm.generate(["Keras is a"])