如何生成最有可能占据给定句子中缺失标记位置的标记列表?

2024-06-25 22:55:52 发布

您现在位置:Python中文网/ 问答频道 /正文

如何生成最有可能占据给定句子中缺失标记位置的标记列表

我发现这个StackOverflow answer,但是,它只生成一个可能的单词,而不是一个适合这个句子的单词列表。我试着打印出每一个变量,看看他是否能生成所有可能的单词,但没有运气

比如说,

>>> sentence = 'Cristiano Ronaldo dos Santos Aveiro GOIH ComM is a Portuguese professional [].' # [] is missing word
>>> generate(sentence)
['soccer', 'basketball', 'tennis', 'rugby']

Tags: answer标记列表is单词stackoverflowsentence句子
2条回答

我刚刚在model hub of HuggingFace上用BERT base uncased模型试用了您的示例,它生成了一个可能的令牌列表:

enter image description here

我可以写一个Colab笔记本来解释如何编写代码。每个神经网络总是输出一个概率分布,因此您可以以最高的概率返回令牌

基本上,您可以执行与this answer中相同的操作,但不只是添加最佳拟合标记,而是以五个最拟合标记为例:

def fill_the_gaps(text):
    text = '[CLS] ' + text + ' [SEP]'
    tokenized_text = tokenizer.tokenize(text)
    indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
    segments_ids = [0] * len(tokenized_text)
    tokens_tensor = torch.tensor([indexed_tokens])
    segments_tensors = torch.tensor([segments_ids])
    with torch.no_grad():
        predictions = model(tokens_tensor, segments_tensors)
    results = []
    for i, t in enumerate(tokenized_text):
        if t == '[MASK]':
            #instead of argmax, we use argsort to sort the tokens which best fit
            predicted_index = torch.argsort(predictions[0, i], descending=True)
            tokens = []
            #the the 5 best fitting tokens and add the to the list
            for k in range(5):
                 predicted_token = tokenizer.convert_ids_to_tokens([predicted_index[k].item()])[0]
                tokens.append(predicted_token)
            results.append(tokens)
    return results

对于您的句子,这将导致:[['footballer', 'golfer', 'football', 'cyclist', 'boxer']]

相关问题 更多 >