Created
November 15, 2019 12:52
-
-
Save analyticsindiamagazine/db871d5fdb9dd83de7cc0791175c5cc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a path to an uncased (all lowercase) version of BERT | |
BERT_MODEL_HUB = "https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1" | |
def create_tokenizer_from_hub_module(): | |
"""Get the vocab file and casing info from the Hub module.""" | |
with tf.Graph().as_default(): | |
bert_module = hub.Module(BERT_MODEL_HUB) | |
tokenization_info = bert_module(signature="tokenization_info", as_dict=True) | |
with tf.Session() as sess: | |
vocab_file, do_lower_case = sess.run([tokenization_info["vocab_file"], | |
tokenization_info["do_lower_case"]]) | |
return bert.tokenization.FullTokenizer( | |
vocab_file=vocab_file, do_lower_case=do_lower_case) | |
tokenizer = create_tokenizer_from_hub_module() | |
# We'll set sequences to be at most 128 tokens long. | |
MAX_SEQ_LENGTH = 128 | |
# Convert our train and validation features to InputFeatures that BERT understands. | |
train_features = bert.run_classifier.convert_examples_to_features(train_InputExamples, label_list, MAX_SEQ_LENGTH, tokenizer) | |
val_features = bert.run_classifier.convert_examples_to_features(val_InputExamples, label_list, MAX_SEQ_LENGTH, tokenizer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment