Skip to content

Instantly share code, notes, and snippets.

@negedng
Created November 15, 2020 11:50
Show Gist options
  • Save negedng/cfba4590ef241cfd90f699dbb2c76278 to your computer and use it in GitHub Desktop.
Save negedng/cfba4590ef241cfd90f699dbb2c76278 to your computer and use it in GitHub Desktop.
import tensorflow_text as text # Registers the ops.
import tensorflow as tf
import tensorflow_hub as hub
# text_input = ["This is a sample sentence."]
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string)
preprocessor = hub.KerasLayer(
"https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/1")
encoder_inputs = preprocessor(text_input) # dict with keys: 'input_mask', 'input_type_ids', 'input_word_ids'
encoder = hub.KerasLayer(
"https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3",
trainable=True)
outputs = encoder(encoder_inputs)
pooled_output = outputs["pooled_output"] # [batch_size, 768].
sequence_output = outputs["sequence_output"] # [batch_size, seq_length, 768].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment