【问题标题】:Bert-multilingual in pytorchpytorch中的Bert-多语言
【发布时间】:2019-06-21 17:51:48
【问题描述】:

我正在对法语文本数据使用 bert 嵌入。我在加载模型和词汇时遇到问题。

我使用以下代码进行标记化,效果很好,但为了获得词汇,它给了我中文单词!!

tokenizer = BertTokenizer.from_pretrained('bert-base-multilingual-cased')
text = "La Banque Nationale du Canada fête cette année le 110e anniversaire de son bureau de Paris."
marked_text = "[CLS] " + text + " [SEP]"
tokenized_text = tokenizer.tokenize(marked_text)
list(tokenizer.vocab.keys())[5000:5020]

我希望词汇表中有法语单词,但我得到了中文单词,我应该在代码中的某处指定语言吗?

【问题讨论】:

  • 据我所知,无法指定语言。

标签: python pytorch multilingual bert-language-model


【解决方案1】:

您正在获取中文文本,因为您正在从词汇表[5000:5020] 中寻找特定范围的单词,它对应于中文文本。此外,bert -base-multilingual-cased 接受了 104 种语言的培训。

如果你想进一步验证你的代码,你可以使用这个:

tokenizer = BertTokenizer.from_pretrained('bert-base-multilingual-cased')
text = "La Banque Nationale du Canada fête cette année le 110e anniversaire de son bureau de Paris."
marked_text = "[CLS] " + text + " [SEP]"
tokenized_text = tokenizer.tokenize(marked_text)

与您的代码相同,后跟:

token_no=[]
for token in tokenized_text:
    #print(tokenizer.vocab[token]) ### you can use this to check the corresponding index of the token
    token_no.append(tokenizer.vocab[token])


### The below code obtains the tokens from the index, which is similar to what you were trying, but on the correct range.
new_token_list=[]
for i in token_no:
    new_token_list.append(list(tokenizer.vocab.keys())[i])

#print(new_token_list); ### you can use it if you want to check back the tokens.

【讨论】:

    猜你喜欢
    • 2022-06-12
    • 2021-02-01
    • 2022-12-13
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2020-04-20
    相关资源
    最近更新 更多