如何使用未标记数据微调 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 (将#修改为@)