Tensorflow bert标记未知单词



我目前正在做以下tf教程:https://www.tensorflow.org/tutorials/text/solve_glue_tasks_using_bert_on_tpu

在不同的句子上测试tokenize函数的输出,我想知道在对未知单词进行tokenize时会发生什么。

加载模型:

bert_model_name = 'bert_en_uncased_L-12_H-768_A-12' 
tfhub_handle_encoder = 'https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3'
tfhub_handle_preprocess = 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3'
bert_preprocess = hub.load(tfhub_handle_preprocess)

句子分词/词:

tok = bert_preprocess.tokenize(tf.constant(['Tensorsss bla']))
print(tok)
# Output:
<tf.RaggedTensor [[[23435, 4757, 2015], [1038, 2721]]]>

难道不应该让每个单词都被标记为单个标记吗?这些显然是由单词组成的,但我想知道当你将这些单词编码为固定长度的向量时会发生什么。

此外,标记器如何将组成的单词转换为3种不同的标记?它会把未知的单词分成不同的已知部分吗?

tensorflow/bert_en_uncased_preprocess/3模型的默认缓存位置是/tmp/tfhub_modules/602d30248ff7929470db09f7385fc895e9ceb4c0(更多关于缓存的信息)。在assets目录中,您将找到vocab.txt,这是使用的词汇表。您可以使用该文件通过查看文件的i+1行来查找令牌idi对应的令牌,即

sed '23436q;d' /tmp/tfhub_modules/602d30248ff7929470db09f7385fc895e9ceb4c0/assets/vocab.txt
> tensor

对所有token-id执行此操作返回

[tensor, ##ss, ##s], [b, ##la]

如你所见,这证实了你的理论,即单词被分成不同的已知部分。关于精确算法的更多细节可以在Subword tokenizers中找到。

最新更新