如何扩展picoGPT功能:添加温度采样和top-p策略的完整教程

张开发
2026/5/5 20:01:41 15 分钟阅读

分享文章

如何扩展picoGPT功能:添加温度采样和top-p策略的完整教程
如何扩展picoGPT功能添加温度采样和top-p策略的完整教程【免费下载链接】picoGPTAn unnecessarily tiny implementation of GPT-2 in NumPy.项目地址: https://gitcode.com/gh_mirrors/pi/picoGPTpicoGPT是一个极简的GPT-2实现项目使用纯NumPy编写代码精简但功能完整。本文将为你展示如何为picoGPT添加温度采样和top-p策略让这个微小的GPT实现具备更强大的文本生成能力。通过本教程你将学会如何扩展picoGPT的采样策略从简单的贪婪采样升级到更灵活的随机采样方法。为什么需要扩展picoGPT的采样策略在阅读gpt2.py和gpt2_pico.py文件时你会发现当前的picoGPT只支持贪婪采样greedy sampling。这意味着每次生成下一个token时它总是选择概率最高的那个token。虽然这种方法简单高效但会导致生成的文本过于确定和重复。温度采样temperature sampling和top-p采样nucleus sampling是两种广泛使用的文本生成策略能够增加文本多样性通过引入随机性生成更具创造性的内容控制生成质量调整温度参数可以平衡生成文本的保守性和创造性避免重复循环防止模型陷入重复的模式理解picoGPT的当前采样实现首先让我们看一下picoGPT当前的采样代码。在gpt2.py的第90-92行logits gpt2(inputs, **params, n_headn_head) # model forward pass next_id np.argmax(logits[-1]) # greedy sampling inputs.append(int(next_id)) # append prediction to input这段代码使用np.argmax()函数从logits中直接选择概率最高的token。这种贪婪采样方法虽然简单但缺乏灵活性。添加温度采样功能温度采样通过调整logits的分布来控制生成文本的随机性。温度值temperature越高生成的文本越随机温度值越低生成的文本越保守。实现温度采样函数我们需要在gpt2.py中添加一个新的采样函数。首先在文件顶部添加必要的导入import numpy as np然后在generate函数之前添加温度采样函数def temperature_sampling(logits, temperature1.0): 温度采样实现 :param logits: 模型输出的原始logits :param temperature: 温度参数控制随机性程度 :return: 采样得到的token id # 应用温度缩放 scaled_logits logits / temperature # 转换为概率分布 probs softmax(scaled_logits) # 从概率分布中采样 next_id np.random.choice(len(probs), pprobs) return next_id修改generate函数支持温度采样接下来我们需要修改generate函数以支持温度采样。首先更新函数签名def generate(inputs, params, n_head, n_tokens_to_generate, temperature1.0): from tqdm import tqdm for _ in tqdm(range(n_tokens_to_generate), generating): logits gpt2(inputs, **params, n_headn_head) # 使用温度采样替代贪婪采样 next_id temperature_sampling(logits[-1], temperature) inputs.append(int(next_id)) return inputs[len(inputs) - n_tokens_to_generate :]更新main函数参数最后更新main函数以接收温度参数def main(prompt: str, n_tokens_to_generate: int 40, model_size: str 124M, models_dir: str models, temperature: float 1.0): from utils import load_encoder_hparams_and_params encoder, hparams, params load_encoder_hparams_and_params(model_size, models_dir) input_ids encoder.encode(prompt) assert len(input_ids) n_tokens_to_generate hparams[n_ctx] # 传递温度参数给generate函数 output_ids generate(input_ids, params, hparams[n_head], n_tokens_to_generate, temperature) output_text encoder.decode(output_ids) return output_text添加top-p采样功能top-p采样也称为nucleus采样是另一种先进的采样策略它从累积概率超过阈值p的最可能token集合中采样。实现top-p采样函数在gpt2.py中添加top-p采样函数def top_p_sampling(logits, top_p0.9): top-pnucleus采样实现 :param logits: 模型输出的原始logits :param top_p: 累积概率阈值 :return: 采样得到的token id # 转换为概率分布 probs softmax(logits) # 按概率降序排序 sorted_indices np.argsort(probs)[::-1] sorted_probs probs[sorted_indices] # 计算累积概率 cumulative_probs np.cumsum(sorted_probs) # 找到累积概率超过top_p的最小token集合 indices_to_keep cumulative_probs top_p if not np.any(indices_to_keep): indices_to_keep[0] True # 限制token集合 sorted_indices sorted_indices[indices_to_keep] sorted_probs sorted_probs[indices_to_keep] # 重新归一化概率 sorted_probs sorted_probs / np.sum(sorted_probs) # 从限制后的分布中采样 next_id np.random.choice(sorted_indices, psorted_probs) return next_id创建组合采样函数为了提供最大的灵活性我们可以创建一个支持多种采样策略的函数def sample_from_logits(logits, strategygreedy, temperature1.0, top_p0.9): 统一的采样函数支持多种采样策略 :param logits: 模型输出的原始logits :param strategy: 采样策略可选 greedy, temperature, top_p, temperature_top_p :param temperature: 温度参数仅用于温度采样 :param top_p: top-p参数仅用于top-p采样 :return: 采样得到的token id if strategy greedy: return np.argmax(logits) elif strategy temperature: return temperature_sampling(logits, temperature) elif strategy top_p: return top_p_sampling(logits, top_p) elif strategy temperature_top_p: # 先应用温度缩放 scaled_logits logits / temperature # 再应用top-p采样 return top_p_sampling(scaled_logits, top_p) else: raise ValueError(fUnknown sampling strategy: {strategy})更新generate函数支持多种策略修改generate函数以支持所有采样策略def generate(inputs, params, n_head, n_tokens_to_generate, strategygreedy, temperature1.0, top_p0.9): from tqdm import tqdm for _ in tqdm(range(n_tokens_to_generate), generating): logits gpt2(inputs, **params, n_headn_head) # 使用指定的采样策略 next_id sample_from_logits(logits[-1], strategy, temperature, top_p) inputs.append(int(next_id)) return inputs[len(inputs) - n_tokens_to_generate :]完整的使用示例现在让我们看看如何使用扩展后的picoGPT# 使用默认的贪婪采样 python gpt2.py 人工智能的未来是 # 使用温度采样温度0.8 python gpt2.py 人工智能的未来是 --temperature 0.8 # 使用top-p采样top_p0.9 python gpt2.py 人工智能的未来是 --strategy top_p --top_p 0.9 # 使用温度top-p组合采样 python gpt2.py 人工智能的未来是 --strategy temperature_top_p --temperature 0.7 --top_p 0.95参数调优建议不同的采样策略和参数组合会产生不同的效果温度参数调优temperature0.1-0.5保守生成输出更确定temperature0.5-1.0平衡的创造性temperature1.0-2.0高度创造性可能产生不连贯内容top-p参数调优top_p0.1-0.5非常保守只考虑最可能的tokentop_p0.7-0.9推荐范围平衡质量和多样性top_p0.95-1.0非常开放考虑几乎所有token性能优化技巧虽然picoGPT本身追求简洁但我们仍然可以做一些优化缓存softmax结果如果多次调用采样函数可以缓存softmax计算结果向量化操作使用NumPy的向量化操作提高效率预计算对于固定的温度值可以预计算缩放因子测试你的实现创建测试脚本验证扩展功能# test_sampling.py import numpy as np # 测试温度采样 def test_temperature_sampling(): logits np.array([1.0, 2.0, 3.0, 4.0]) # 测试不同温度值 for temp in [0.1, 0.5, 1.0, 2.0]: samples [] for _ in range(1000): sample temperature_sampling(logits, temp) samples.append(sample) # 验证采样分布 unique, counts np.unique(samples, return_countsTrue) print(fTemperature{temp}: {dict(zip(unique, counts))}) # 运行测试 if __name__ __main__: test_temperature_sampling()总结通过本教程你已经成功为picoGPT添加了温度采样和top-p策略功能。这些扩展使得picoGPT从一个简单的贪婪采样实现变成了一个功能更完整的文本生成工具。记住采样策略的选择对生成文本的质量和多样性有重要影响建议根据具体应用场景调整参数。扩展后的picoGPT现在支持✅ 贪婪采样原始功能✅ 温度采样控制随机性✅ top-p采样控制token集合大小✅ 温度top-p组合采样最佳实践这些改进让picoGPT在保持极简代码风格的同时提供了与大型语言模型相媲美的采样灵活性。现在你可以使用这个增强版的picoGPT来生成更具创造性和多样性的文本内容了【免费下载链接】picoGPTAn unnecessarily tiny implementation of GPT-2 in NumPy.项目地址: https://gitcode.com/gh_mirrors/pi/picoGPT创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

更多文章