【问题标题】:How to use my own corpus on word embedding model BERT如何在词嵌入模型 BERT 上使用我自己的语料库
【发布时间】:2021-03-25 23:31:44
【问题描述】:

我正在尝试使用来自谷歌的词嵌入模型 BERT 创建一个问答模型。我对此并不陌生,并且真的很想使用我自己的语料库进行培训。起初我使用了来自huggingface site 的示例,效果很好:

from transformers import pipeline

qa_pipeline = pipeline(
    "question-answering",
    model="henryk/bert-base-multilingual-cased-finetuned-dutch-squad2",
    tokenizer="henryk/bert-base-multilingual-cased-finetuned-dutch-squad2"
)

qa_pipeline({
    'context': "Amsterdam is de hoofdstad en de dichtstbevolkte stad van Nederland.",
    'question': "Wat is de hoofdstad van Nederland?"})

输出

> {'answer': 'Amsterdam', 'end': 9, 'score': 0.825619101524353, 'start': 0}

所以,我尝试创建一个 .txt 文件来测试是否可以将上下文参数中的句子与完全相同的句子互换,但在 .txt 文件中。

with open('test.txt') as f:
    lines = f.readlines()

qa_pipeline = pipeline(
    "question-answering",
    model="henryk/bert-base-multilingual-cased-finetuned-dutch-squad2",
    tokenizer="henryk/bert-base-multilingual-cased-finetuned-dutch-squad2"
)

qa_pipeline({
    'context': lines,
    'question': "Wat is de hoofdstad van Nederland?"})

但这给了我以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-2bae0ecad43e> in <module>()
     10 qa_pipeline({
     11     'context': lines,
---> 12     'question': "Wat is de hoofdstad van Nederland?"})

5 frames
/usr/local/lib/python3.6/dist-packages/transformers/data/processors/squad.py in _is_whitespace(c)
     84 
     85 def _is_whitespace(c):
---> 86     if c == " " or c == "\t" or c == "\r" or c == "\n" or ord(c) == 0x202F:
     87         return True
     88     return False

TypeError: ord() expected a character, but string of length 66 found

我只是在尝试阅读和使用 .txt 文件的方法,但我似乎没有找到不同的解决方案。我对 huggingface pipeline() 函数做了一些研究,这是关于问题和上下文参数的内容:

【问题讨论】:

    标签: word-embedding bert-language-model huggingface-transformers


    【解决方案1】:

    知道了!解决方案非常简单。我假设变量 'lines' 已经是一个 str 但事实并非如此。只需转换为字符串,问答模型就接受了我的 test.txt 文件。

    所以来自:

    with open('test.txt') as f:
        lines = f.readlines()
    

    到:

    with open('test.txt') as f:
        lines = str(f.readlines())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 2018-08-04
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多