微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用未标记数据微调 GPT2 语言模型

如何解决如何使用未标记数据微调 GPT2 语言模型

我正在使用 GPT2 创建语言模型(即下一个词预测器)。我已关注此博客 https://www.analyticsvidhya.com/blog/2019/08/comprehensive-guide-language-model-nlp-python-code/ 并使用此代码创建语言模型。

# Import required libraries
import torch
from pytorch_transformers import GPT2Tokenizer,GPT2LMHeadModel

# Load pre-trained model tokenizer (vocabulary)
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')

# Encode a text inputs
text = "What is the fastest car in the"
indexed_tokens = tokenizer.encode(text)

# Convert indexed tokens in a PyTorch tensor
tokens_tensor = torch.tensor([indexed_tokens])

# Load pre-trained model (weights)
model = GPT2LMHeadModel.from_pretrained('gpt2')

# Set the model in evaluation mode to deactivate the DropOut modules
model.eval()

# If you have a GPU,put everything on cuda
tokens_tensor = tokens_tensor.to('cuda')
model.to('cuda')

# Predict all tokens
with torch.no_grad():
outputs = model(tokens_tensor)
predictions = outputs[0]

# Get the predicted next sub-word
predicted_index = torch.argmax(predictions[0,-1,:]).item()
predicted_text = tokenizer.decode(indexed_tokens + [predicted_index])

# Print the predicted word
print(predicted_text)

现在我想知道如何微调模型或进行迁移学习以在我自己的数据集上训练模型。我有一个与药物和医疗保健相关的数据集。所以,我希望我的模型根据我的数据集进行下一个词的预测。

希望你明白我想做什么。任何形式的帮助将不胜感激。提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。