我正在尝试编写一个程序,给定一个句子列表,返回最可能的一个。我想使用 GPT-2,但我对使用它还很陌生(因为我真的不知道该怎么做)。我计划在给定前面的单词的情况下查找单词的概率,并将所有概率相乘以获得该句子出现的总体概率,但是我不知道如何在给定前面的单词的情况下查找单词出现的概率。这是我的(伪)代码:
sentences = # my list of sentences
max_prob = 0
best_sentence = sentences[0]
for sentence in sentences:
prob = 1 #probability of that sentence
for idx, word in enumerate(sentence.split()[1:]):
prob *= probability(word, " ".join(sentence[:idx])) # this is where I need help
if prob > max_prob:
max_prob = prob
best_sentence = sentence
print(best_sentence)
Run Code Online (Sandbox Code Playgroud)
我可以帮忙吗?