First, download the model
(this may not be necessary since the model will be automatically downloaded using from_pretrained()):
$ huggingface-cli download microsoft/deberta-v3-xsmall
Python code:
$ vi token-embeddings.py
from transformers import AutoModel, AutoTokenizer
# load model and tokenizer
model = AutoModel.from_pretrained("microsoft/deberta-v3-xsmall")
tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-base")
# tokenize input text
tokens = tokenizer('Hello Embeddings!', return_tensors='pt')
# decode tokens to see how text was split
for token in tokens['input_ids'][0]:
# convert the input token id to it corresponding token
print(tokenizer.decode(token))
# generate embeddings
output = model(**tokens)[0]
# shape: [batch_size, number_of_tokens, embeddings_dimension]
print(output.shape) # torch.Size([1, 7, 384])
# output embeddings
print(output)
Run the Python script:
$ python3 token-embeddings.py
Output:
# input tokens
[CLS]
Hello
Emb
edd
ings
!
[SEP]
# output shape: [batch_size, number_of_tokens, embeddings_dimension]
torch.Size([1, 7, 384])
# output embeddings
tensor([[[-3.3186, 0.1003, -0.1506, ..., -0.2840, -0.3882, -0.1670],
[-0.5446, 0.7986, -0.4200, ..., 0.1163, -0.3322, -0.3622],
[-0.1689, 0.6443, -0.0145, ..., 0.0207, -0.5754, 1.3607],
...,
[ 0.0366, 0.0818, -0.0607, ..., -0.4793, -0.7831, -0.9185],
[-0.0555, 0.3136, 0.2662, ..., 0.3092, -0.4876, -0.3294],
[-3.1255, 0.1324, -0.0899, ..., -0.1426, -0.5295, 0.0731]]],
grad_fn=<NativeLayerNormBackward0>)
Note that the created embeddings have the size "1, 7, 384" (may vary based on input and tokenization):
- 1: the batch dimension
- 7: seven tokens
- 384: each token is embedded in a vector of 384 values
The batch dimension can be larger than 1 in cases when multiple sentences are given to the model to be processed at the same time.