我正在使用 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)
现在我想知道如何微调模型或进行迁移学习以在我自己的数据集上训练模型。我有一个与药物和医疗保健相关的数据集。所以,我希望我的模型根据我的数据集进行下一个单词预测。
希望你明白我想做什么。任何形式的帮助将不胜感激。提前致谢。