大家好,我是python222_小锋老师,最近更新《AI大模型应用开发入门-拥抱Hugging Face与Transformers生态》专辑,感谢大家支持。
本课程主要介绍和讲解Hugging Face和Transformers,包括加载预训练模型,自定义数据集,模型推理,模型微调,模型性能评估等。是AI大模型应用开发的入门必备知识。
基于GPT-2文本生成模型微调 - GPT-2中文文本生成模型实例
1,gpt2-chinese-cluecorpussmall 通用文本生成实例
示例代码:
import torch from transformers import AutoTokenizer, AutoModelForCausalLM def test_text_generation(): # 使用设备(GPU/CPU) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # print(device) # 加载分词器 tokenizer = AutoTokenizer.from_pretrained('../gpt2-chinese-cluecorpussmall') # 加载模型 model = AutoModelForCausalLM.from_pretrained('../gpt2-chinese-cluecorpussmall') # print(model) # 设置为评估模式 model.eval() model.to(device) # 准备输入数据 input_ids = tokenizer.encode( text='大语言模型技术发展', # 输入文本 return_tensors='pt' # 返回PyTorch张量 ).to(device) # 生成文本 output_sequences = model.generate( input_ids=input_ids, max_length=100, # 生成的文本总长度 num_return_sequences=1, # 返回的生成序列数量 no_repeat_ngram_size=2, # 避免重复的n-gram 防止相同词组重复出现,从而提高生成文本的多样性和自然性。 temperature=0.7, # 温度参数控制随机性 top_k=50, # 仅从前k个概率最高的单词中采样 top_p=0.95, # 只从前95%概率质量的词汇中进行随机采样 核采样策略 do_sample=True # 开启采样 ) # print(output_sequences) # 解码并打印生成的文本 for sequence in output_sequences: generated_text = tokenizer.decode(sequence, skip_special_tokens=True) print(generated_text) if __name__ == '__main__': for i in range(3): test_text_generation()运行结果:
2,gpt2-chinese-ancient 古文生成实例
示例代码:
import torch from transformers import AutoTokenizer, AutoModelForCausalLM def test_text_generation(): # 使用设备(GPU/CPU) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # print(device) # 加载分词器 tokenizer = AutoTokenizer.from_pretrained('../gpt2-chinese-ancient') # 加载模型 model = AutoModelForCausalLM.from_pretrained('../gpt2-chinese-ancient') # print(model) # 设置为评估模式 model.eval() model.to(device) # 准备输入数据 input_ids = tokenizer.encode( text='悠哉', # 输入文本 return_tensors='pt' # 返回PyTorch张量 ).to(device) # 生成文本 output_sequences = model.generate( input_ids=input_ids, max_length=100, # 生成的文本总长度 num_return_sequences=1, # 返回的生成序列数量 no_repeat_ngram_size=2, # 避免重复的n-gram 防止相同词组重复出现,从而提高生成文本的多样性和自然性。 temperature=0.7, # 温度参数控制随机性 top_k=50, # 仅从前k个概率最高的单词中采样 top_p=0.95, # 只从前95%概率质量的词汇中进行随机采样 核采样策略 do_sample=True # 开启采样 ) # print(output_sequences) # 解码并打印生成的文本 for sequence in output_sequences: generated_text = tokenizer.decode(sequence, skip_special_tokens=True) print(generated_text) if __name__ == '__main__': for i in range(3): test_text_generation()运行结果:
3,gpt2-chinese-couplet 对联生成实例
实例代码:
import torch from transformers import AutoTokenizer, AutoModelForCausalLM def test_text_generation(): # 使用设备(GPU/CPU) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # print(device) # 加载分词器 tokenizer = AutoTokenizer.from_pretrained('../gpt2-chinese-couplet') # 加载模型 model = AutoModelForCausalLM.from_pretrained('../gpt2-chinese-couplet') # print(model) # 设置为评估模式 model.eval() model.to(device) # 准备输入数据 input_ids = tokenizer.encode( text='春满神州花似锦-', # 输入文本 return_tensors='pt' # 返回PyTorch张量 ).to(device) # 生成文本 output_sequences = model.generate( input_ids=input_ids, max_length=19, # 生成的文本总长度 num_return_sequences=1, # 返回的生成序列数量 no_repeat_ngram_size=2, # 避免重复的n-gram 防止相同词组重复出现,从而提高生成文本的多样性和自然性。 temperature=0.7, # 温度参数控制随机性 top_k=50, # 仅从前k个概率最高的单词中采样 top_p=0.95, # 只从前95%概率质量的词汇中进行随机采样 核采样策略 do_sample=True # 开启采样 ) print(output_sequences) # 解码并打印生成的文本 for sequence in output_sequences: generated_text = tokenizer.decode(sequence, skip_special_tokens=True) print(generated_text) if __name__ == '__main__': for i in range(3): test_text_generation()运行结果:
4,gpt2-chinese-poem 古典诗词生成实例
实例代码:
import torch from transformers import AutoTokenizer, AutoModelForCausalLM def test_text_generation(): # 使用设备(GPU/CPU) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # print(device) # 加载分词器 tokenizer = AutoTokenizer.from_pretrained('../gpt2-chinese-poem') # 加载模型 model = AutoModelForCausalLM.from_pretrained('../gpt2-chinese-poem') # print(model) # 设置为评估模式 model.eval() model.to(device) # 准备输入数据 input_ids = tokenizer.encode( text='床前明月光,', # 输入文本 return_tensors='pt' # 返回PyTorch张量 ).to(device) # 生成文本 output_sequences = model.generate( input_ids=input_ids, max_length=35, # 生成的文本总长度 num_return_sequences=1, # 返回的生成序列数量 no_repeat_ngram_size=2, # 避免重复的n-gram 防止相同词组重复出现,从而提高生成文本的多样性和自然性。 temperature=0.7, # 温度参数控制随机性 top_k=50, # 仅从前k个概率最高的单词中采样 top_p=0.95, # 只从前95%概率质量的词汇中进行随机采样 核采样策略 do_sample=True # 开启采样 ) # print(output_sequences) # 解码并打印生成的文本 for sequence in output_sequences: generated_text = tokenizer.decode(sequence, skip_special_tokens=True) print(generated_text) if __name__ == '__main__': for i in range(3): test_text_generation()运行结果:
5,gpt2-chinese-lyric 歌词创作生成实例
示例代码:
import torch from transformers import AutoTokenizer, AutoModelForCausalLM def test_text_generation(): # 使用设备(GPU/CPU) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # print(device) # 加载分词器 tokenizer = AutoTokenizer.from_pretrained('../gpt2-chinese-lyric') # 加载模型 model = AutoModelForCausalLM.from_pretrained('../gpt2-chinese-lyric') # print(model) # 设置为评估模式 model.eval() model.to(device) # 准备输入数据 input_ids = tokenizer.encode( text='我爱你,就像老鼠太大米。', # 输入文本 return_tensors='pt' # 返回PyTorch张量 ).to(device) # 生成文本 output_sequences = model.generate( input_ids=input_ids, max_length=300, # 生成的文本总长度 num_return_sequences=1, # 返回的生成序列数量 no_repeat_ngram_size=2, # 避免重复的n-gram 防止相同词组重复出现,从而提高生成文本的多样性和自然性。 temperature=0.7, # 温度参数控制随机性 top_k=50, # 仅从前k个概率最高的单词中采样 top_p=0.95, # 只从前95%概率质量的词汇中进行随机采样 核采样策略 do_sample=True # 开启采样 ) # print(output_sequences) # 解码并打印生成的文本 for sequence in output_sequences: generated_text = tokenizer.decode(sequence, skip_special_tokens=True) print(generated_text) if __name__ == '__main__': for i in range(3): test_text_generation()运行结果: