文章预览
在大型语言模型(LLM)的迷人世界中,模型架构、数据处理和优化常常成为关注的焦点。但解码策略在文本生成中扮演着至关重要的角色,却经常被忽视。 在这篇文章中,我们将通过深入探讨贪婪搜索和束搜索的机制,以及采用顶K采样和核采样的技术,来探索LLM是如何生成文本的。 https://mlabonne.github.io/blog/posts/2022-06-07-Decoding_strategies.html https://colab.research.google.com/drive/19CJlOS5lI29g-B3dziNn93Enez1yiHk2?usp=sharing unset unset 基础知识 unset unset 为了开始,我们先举一个例子。我们将文本“I have a dream”输入到GPT-2模型中,并让它生成接下来的五个词(单词或子词)。 from transformers import GPT2LMHeadModel, GPT2Tokenizer import torch device = 'cuda' if torch.cuda.is_available() else 'cpu' model = GPT2LMHeadModel.from_pretrained( 'gpt2' ).to(device) tokenizer = GPT2Tokenizer.from_pretrained( 'gpt2' ) mode
………………………………