import torch import torch.nn.functional as F from transformers import AutoModelForCausalLM, AutoTokenizer from transformers.generation.logits_process import RepetitionPenaltyLogitsProcessor
输入文本: I love coding. I love 原始输出 (无repetition penalty) -------------------------------------------------- Token Raw Score Probability -------------------------------------------------- the 16.583 0.176431 to 15.963 0.094929 learning 15.550 0.062831 solving 15.482 0.058693 programming 15.221 0.045199
应用repetition penalty = 0.8后的输出 -------------------------------------------------- Token Raw Score Probability -------------------------------------------------- coding 18.377 0.519966 the 16.583 0.086431 to 15.963 0.046504 learning 15.550 0.030780 solving 15.482 0.028753
应用repetition penalty = 1.2后的输出 -------------------------------------------------- Token Raw Score Probability -------------------------------------------------- the 16.583 0.180972 to 15.963 0.097372 learning 15.550 0.064449 solving 15.482 0.060203 programming 15.221 0.046362
应用repetition penalty = 2.0后的输出 -------------------------------------------------- Token Raw Score Probability -------------------------------------------------- the 16.583 0.181423 to 15.963 0.097615 learning 15.550 0.064609 solving 15.482 0.060353 programming 15.221 0.046477
可以看到,通过设置repetition_penality 为0.8后,预测的概率最大token为coding,概率为0.519966,而设置repetition_penality为1.2和2.0,提高了预测token the 的出现概率。