Как получить вокаб-файл для токенизатора Берта из TF Hub - PullRequest
0 голосов
/ 09 января 2020

Я пытаюсь использовать Bert из TensorFlow Hub и построить токенизатор, вот что я делаю:

>>> import tensorflow_hub as hub
>>> from bert.tokenization import FullTokenizer

>>> BERT_URL = 'https://tfhub.dev/tensorflow/bert_zh_L-12_H-768_A-12/1'
>>> bert_layer = hub.KerasLayer(BERT_URL, trainable=False)
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1781: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1781: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.

Но теперь, когда я проверяю файл vocab в разрешенных объектах, я получаю пустой тензор

>>> bert_layer.resolved_object.vocab_file.asset_path.shape
TensorShape([])

Как правильно получить этот файл Vocab?

1 Ответ

0 голосов
/ 06 апреля 2020

Попробуйте:

FullTokenizer = bert.bert_tokenization.FullTokenizer
bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1", trainable=False)

vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy() #The vocab file of bert for tokenizer
tokenizer = FullTokenizer(vocab_file)

Затем вы можете токенизироваться с помощью токенизатора.

tokenizer.tokenize('Where are you going?') 

['w', '## hee', '## re', 'are', 'you', 'собираетесь', '?']

Вы можете также передайте другие функции в ваш токенизатор. Например:

do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = FullTokenizer(vocab_file, do_lower_case) 
tokenizer.tokenize('Where are you going?')

['где', 'are', 'you', 'собираетесь', '?']

...