参考资料:实验楼的《探究bert的阅读机理》
模型的加载
# 加载模型
model = DistilBertForQuestionAnswering.from_pretrained(MODEL_PATH)
# 将模型移到相应设备
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(DEVICE) # 查看模型结构
基于同样的路径,加载 tokenizer:
# 加载 tokenizer,用于处理训练数据
from transformers import DistilBertTokenizerFast
# 初始化tokenizer
tokenizer = DistilBertTokenizerFast.from_pretrained(MODEL_PATH)
定义测试的问题与上下文
question = "in 2011, Beyonce performed for four nights where?"
context = "Her fourth studio album 4 was released on June 28, 2011 in the US. 4 sold 310,000 copies in its first week and debuted atop the Billboard 200 chart, giving Beyonc\u00e9 her fourth consecutive number-one album in the US. The album was preceded by two of its singles \"Run the World (Girls)\" and \"Best Thing I Never Had\", which both attained moderate success. The fourth single \"Love on Top\" was a commercial success in the US. 4 also produced four other singles; \"Party\", \"Countdown\", \"I Care\" and \"End of Time\". \"Eat, Play, Love\", a cover story written by Beyonc\u00e9 for Essence that detailed her 2010 career break, won her a writing award from the New York Association of Black Journalists. In late 2011, she took the stage at New