使用Transformers生成文本
Tokenizer基础用法
from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
result = tokenizer.tokenize("I have a new GPU!")
print(result)
result = tokenizer.encode("I have a new GPU!")
print(result)
result = tokenizer.decode(result)
print(result)
输出:
['i', 'have', 'a', 'new', 'gp', '##u', '!']
[101, 1045, 2031, 1037, 2047, 14246, 2226, 999, 102]
[CLS] i have a new gpu! [SEP]生成文本(使用流水线API)
模型下载1:openai-community/gpt2 · Hugging Face
模型下载2:openai-community/gpt2 · HF Mirror
from transformers import pipeline, set_seed
generator = pipeline("text-generation", model="gpt2")
set_seed(42)
result = generator(
"Hello, I'm a language model,", max_length=30, num_return_sequences=5
)
print(result)
输出:
[{'generated_text': "Hello, I'm a language model, so that makes a lot of sense. (laughter) That's why it comes here, that's why I"}, {'generated_text': 'Hello, I\'m a language model, my brain is a model. Maybe I should stop pretending that I understand human language and start practicing it." And'}, {'generated_text': 'Hello, I\'m a language model, I\'m not a programmer. This is good, you understand.\n\n"But there is something that you'}, {'generated_text': "Hello, I'm a language model, and that's why I feel so strongly about this game.\n\nI think games don't make a lot"}, {'generated_text': 'Hello, I\'m a language model, it\'s called a macro. But I don\'t really understand them yet."\n\nWhile the
program is very'}]生成文本(不使用流水线API)
模型下载1:openai-community/gpt2 · Hugging Face
模型下载2:openai-community/gpt2 · HF Mirror
from transformers import GPT2Tokenizer, GPT2LMHeadModel
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")
text = "long long ago, there was a"
encoded_input = tokenizer(text, return_tensors="pt")
output_ids = model.generate(**encoded_input)
generated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
print(generated_text)
输出:
long long ago, there was a time when the world was a little bit more peaceful.
"I'm sorry, but I生成文本(使用流水线API)
模型下载1:facebook/bart-large-mnli · Hugging Face
模型下载2:facebook/bart-large-mnli · HF Mirror
from transformers import pipeline
classifier = pipeline(model="facebook/bart-large-mnli")
result = classifier(
"I have a problem with my iphone that needs to be resolved asap!!",
candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"],
)
print(result)
输出:
{'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'], 'scores': [0.5036358833312988, 0.47879910469055176, 0.012600448913872242, 0.0026557832024991512, 0.002308770315721631]}生成文本(使用流水线API)
模型下载1:facebook/opt-1.3b · Hugging Face
模型下载2:facebook/opt-1.3b · HF Mirror
安装包:
pip install accelerate bitsandbytesimport torch
from transformers import pipeline
pipe = pipeline(
model="facebook/opt-1.3b", device_map="auto", model_kwargs={"load_in_8bit": True}
)
output = pipe("This is a cool example!", do_sample=True, top_p=0.95)
print(output)输出:
{'generated_text': 'This is a cool example! The idea for this is a little far fetched but if you'}] 最后更新于10月前
本文由人工编写,AI优化,转载请注明原文地址: 使用Transformers生成文本
推荐阅读
VMware Workstation 16激活码及许可证密钥获取方法
20712025-10-26
天地图429 Too Many Requests错误解决方案-配额升级与配置指南
4262025-11-25
OpenAI Codex命令行工具安装与使用教程:AI编程助手实战指南
14982025-10-08
Windows系统PyTorch安装教程:CUDA 12.1环境配置与TorchText版本兼容性指南
22592025-10-08
VMware Workstation 17许可证密钥及免费激活方法详解
25942025-10-26
机器学习框架全面指南:从入门到实战应用
2842025-11-23
评论 (0)
发表评论
昵称:加载中...
暂无评论,快来发表第一条评论吧!