【问题标题】:How to get the corresponding character or string that has been labelled as 'UNK' token in BERT?如何获取BERT中已标记为'UNK'标记的对应字符或字符串?
【发布时间】:2021-12-12 10:08:33
【问题描述】:

在对字符串进行标记化后,它会返回由单独的单词和特殊标记组成的标记列表。例如,如果有的话,如何解码哪个单词/字符被称为 'UNK' 标记?

【问题讨论】:

    标签: python huggingface-transformers bert-language-model huggingface-tokenizers


    【解决方案1】:

    快速分词器返回一个 Batchencoding 对象,该对象具有内置的 word_idstoken_to_chars

    from transformers import BertTokenizerFast
    
    t = BertTokenizerFast.from_pretrained('bert-base-uncased')
    
    tokens = t('word embeddings are vectors ?')
    print(tokens['input_ids'])
    print(t.decode(tokens['input_ids']))
    print(tokens.word_ids())
    print(tokens.token_to_chars(8))
    

    输出:

    [101, 2773, 7861, 8270, 4667, 2015, 2024, 19019, 100, 102]
    [CLS] word embeddings are vectors [UNK] [SEP]
    [None, 0, 1, 1, 1, 1, 2, 3, 4, None]
    CharSpan(start=28, end=29)
    
    

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多