import tensorflow as tf
from transformers import AutoTokenizer, TFBertModel, BertConfig
# Load the tokenizer, model and the model config
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased")
model_config = BertConfig.from_pretrained("bert-large-uncased")
model = TFBertModel.from_pretrained("bert-large-uncased",
config=model_config,)
# Prepare inputs by tokenizing the examples
inputs = tokenizer("My puppy is very cute!", return_tensors="tf")
@tf.function
def generate():
return model(**inputs)
# Generate outputs
outputs = generate()
# Get last hidden states
last_hidden_states = outputs.last_hidden_state
# Print the shape of the last hidden states
print("Last hidden states shape:", last_hidden_states.shape)
Sample
Output
Last hidden states shape: (1, 8, 1024)