对比采样器

[源文件]

ContrastiveSampler

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

对比采样器类。

这个采样器实现了对比搜索算法。简而言之,采样器选择具有最高“得分”的token作为下一个token。“得分”是token的概率与对照先前token的最大相似度之间的加权和。通过使用这个联合得分,对比采样器减少了重复已见token的行为。

参数

  • k: int,top-k的k值。下一个token将从k个token中选择。
  • 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"])