bert_vocab.bert_vocab_from_dataset 返回错误的词汇

问题描述 投票:0回答:0

我正在尝试按照 tf 的教程构建分词器 https://www.tensorflow.org/text/guide/subwords_tokenizer。我基本上只是用不同的数据集做同样的事情。有问题的数据集是一个 txt 文件,其中前两列是英语句子或单词和意大利语翻译,这里是一个片段:

Hi. Ciao!   CC-BY 2.0 (France) Attribution: tatoeba.org #538123 (CM) & #607364 (Cero)
Hi. Ciao.   CC-BY 2.0 (France) Attribution: tatoeba.org #538123 (CM) & #4522287 (Guybrush88)
Run!    Corri!  CC-BY 2.0 (France) Attribution: tatoeba.org #906328 (papabear) & #906347 (Guybrush88)
Run!    Corra!  CC-BY 2.0 (France) Attribution: tatoeba.org #906328 (papabear) & #906348 (Guybrush88)
Run!    Correte!    CC-BY 2.0 (France) Attribution: tatoeba.org #906328 (papabear) & #906350 (Guybrush88)
Who?    Chi?    CC-BY 2.0 (France) Attribution: tatoeba.org #2083030 (CK) & #2126402 (Guybrush88)

可以在http://www.manythings.org/anki/

下载

我已经对其进行了预处理,并将英语和意大利语句子转换为张量流数据集,以提供给分词器,如以下代码所示:

import tensorflow as tf
from tensorflow_text.tools.wordpiece_vocab import bert_vocab_from_dataset as bert_vocab
import tensorflow_text as tf_text
import os
import numpy as np

eng_dataset, ita_dataset = np.genfromtxt('ita_eng_dataset.txt',
                                         usecols=(0, 1),
                                         encoding='utf-8',
                                         unpack=True,
                                         dtype='str')

eng_dataset_tensor = tf.convert_to_tensor(eng_dataset)
ita_dataset_tensor = tf.convert_to_tensor(ita_dataset)

eng_tf_dataset = tf.data.Dataset.from_tensor_slices(eng_dataset_tensor)
ita_tf_dataset = tf.data.Dataset.from_tensor_slices(ita_dataset_tensor)

当我尝试将它喂给

bert_vocab_from_dataset
时出现问题:

bert_tokenizer_params = dict(lower_case=True)
reserved_tokens = ["[PAD]", "[UNK]", "[START]", "[END]"]

bert_vocab_args = dict(
    # The target vocabulary size
    vocab_size=8000,
    # Reserved tokens that must be included in the vocabulary
    reserved_tokens=reserved_tokens,
    # Arguments for `text.BertTokenizer`
    bert_tokenizer_params=bert_tokenizer_params,
    # Arguments for `wordpiece_vocab.wordpiece_tokenizer_learner_lib.learn`
    learn_params={},
)

eng_vocab = bert_vocab.bert_vocab_from_dataset(eng_tf_dataset, **bert_vocab_args)
ita_vocab = bert_vocab.bert_vocab_from_dataset(ita_tf_dataset, **bert_vocab_args)

但是结果是错误的:

print(eng_vocab[:20])
print(ita_vocab[1980:2000])
print(len(eng_vocab), len(ita_vocab))

输出

['about', 'breakfast', 'coffee', 'correct', 'finally', 'heat', 'japanese', 'large', 'lie', 'old', 'peel', 'science', 'step', 'swimming', 'work', '##ans', '##b', '##der', '##ins', '##ish']
['##omfortable', '##ong', '##ony', '##op', '##ouse', '##ply', '##rch', '##rous', '##rove', '##roved', '##sists', '##tained', '##ten', '##unted', '##val', '##ze', 'advice', 'agitated', 'amazed', 'argued']
665 2413

正如你所看到的,意大利语词汇包含英文文本并且都非常少(这可能是由于数据集,但看起来很奇怪这么小,甚至不到 1000 个词汇)。

我也尝试像在 tensorflow 教程中那样对输入数据集进行批处理,但它给出了相同的结果。

我在 Windows 11 和 tensorflow 2.10 的 pycharm 上使用 python 3.8

python tensorflow deep-learning tokenize bert-language-model
© www.soinside.com 2019 - 2024. All rights reserved.