Views
No views yet
input_word_ids, input_mask, and input_type_ids.preprocessor_path = snapshot_download(repo_id="Dimitre/bert_en_cased_preprocess")
preprocessor = KerasLayer(handle=preprocessor_path)
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string)
encoder_inputs = preprocessor(text_input)
model_path = snapshot_download(repo_id="Dimitre/bert_en_cased_L-12_H-768_A-12")
encoder = KerasLayer(handle=model_path, trainable=True)
outputs = encoder(encoder_inputs)
pooled_output = outputs["pooled_output"] # [batch_size, 768].
sequence_output = outputs["sequence_output"] # [batch_size, seq_length, 768].preprocessor = pull_from_hub(repo_id="Dimitre/bert_en_cased_preprocess")
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string)
encoder_inputs = preprocessor(text_input)
encoder = pull_from_hub(repo_id="Dimitre/bert_en_cased_L-12_H-768_A-12", trainable=True)
outputs = encoder(encoder_inputs)
pooled_output = outputs["pooled_output"] # [batch_size, 768].
sequence_output = outputs["sequence_output"] # [batch_size, seq_length, 768].pooled_output to represents each input sequence as a whole, and the sequence_output to represent each input token in context. Either of those can be used as input to further model building.embedding_model = tf.keras.Model(text_input, pooled_output)
sentences = tf.constant(["(your text here)"])
print(embedding_model(sentences))seq_length.outputs["encoder_outputs"][i] is a Tensor of shape [batch_size, seq_length, 768] with the outputs of the i-th Transformer block, for 0 <= i < L. The last value of the list is equal to sequence_output.tf.data.Dataset.map() while this encoder stays a part of a larger model that gets trained on that dataset. The Keras input objects for running on preprocessed inputs areencoder_inputs = dict(
input_word_ids=tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32),
input_mask=tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32),
input_type_ids=tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32),
).mlm subobject with predictions for the Masked Language Model task it was originally trained with. This allows advanced users to continue MLM training for fine-tuning to a downstream task. It extends the encoder interface above with a zero-padded tensor of positions in the input sequence for which the input_word_ids have been randomly masked or altered. (See the preprocessor model page for how to get the id of the mask token and more.)mlm_inputs = dict(
input_word_ids=tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32),
input_mask=tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32),
input_type_ids=tf.keras.layers.Input(shape=(seq_length,), dtype=tf.int32),
masked_lm_positions=tf.keras.layers.Input(shape=(num_predict,), dtype=tf.int32),
)
encoder = pull_from_hub(repo_id="Dimitre/bert_en_cased_L-12_H-768_A-12")
mlm = hub.KerasLayer(encoder.mlm, trainable=True)
mlm_outputs = mlm(mlm_inputs)
mlm_logits = mlm_outputs["mlm_logits"] # [batch_size, num_predict, vocab_size]
# ...plus pooled_output, sequence_output and encoder_outputs as above.