gpt-2模型训练后应用

一、生成的文件集:

根据上篇文章训练后的数据进行训练后,使用训练好的数据集:

二、代码样例实现:

复制代码
from transformers import GPT2Tokenizer, GPT2LMHeadModel


tokenizer = GPT2Tokenizer.from_pretrained('./gpt2-model')
model = GPT2LMHeadModel.from_pretrained('./gpt2-model')

tokenizer.save_pretrained('./results/tokenizer')
model.save_pretrained('./results/model')

# 加载训练好的模型和tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('./results/tokenizer')
model = GPT2LMHeadModel.from_pretrained('./results/model')

# 生成回答示例
question = "什么是高血压的最佳治疗方法?"
input_text = f"提问: {question}\n回答:"
input_ids = tokenizer.encode(input_text, return_tensors='pt')

output = model.generate(input_ids, max_length=100, top_k=50, top_p=0.95, temperature=0.7, do_sample=True)
answer = tokenizer.decode(output[0], skip_special_tokens=True).split("回答:")[1].strip()

print("问题:", question)
print("回答:", answer)
相关推荐
花酒锄作田3 小时前
Pydantic校验配置文件
python
hboot3 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi14 小时前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi15 小时前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽15 小时前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户83580861879116 小时前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L1 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅1 天前
海天线算法的前世今生
python·计算机视觉
韩师傅1 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L1 天前
LangGraph的MessageState and HumanMessage
python