Skip to content

Commit

Permalink
Create Embeddings_PyT_TF.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RubensZimbres authored May 25, 2021
1 parent 11aec67 commit 616c332
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Embeddings_PyT_TF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Using Pytorch:

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute")).unsqueeze(0) # Batch size 1
outputs = model(input_ids)
last_hidden_states = outputs[0] # The last hidden-state is the first element of the output tuple
Using Tensorflow:

import tensorflow as tf
from transformers import BertTokenizer, TFBertModel

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = TFBertModel.from_pretrained('bert-base-uncased')
input_ids = tf.constant(tokenizer.encode("Hello, my dog is cute"))[None, :] # Batch size 1
outputs = model(input_ids)
last_hidden_states = outputs[0] # The last hidden-state is the first element of the output tuple

0 comments on commit 616c332

Please sign in to comment.