I’m going over the huggingface tutorial where they showed how tokens can be fed into a model to generate hidden representations:
JavaScript
x
14
14
1
import torch
2
from transformers import RobertaTokenizer
3
from transformers import RobertaModel
4
5
checkpoint = 'roberta-base'
6
tokenizer = RobertaTokenizer.from_pretrained(checkpoint)
7
model = RobertaModel.from_pretrained(checkpoint)
8
9
sequences = ["I've been waiting for a HuggingFace course my whole life."]
10
11
tokens = tokenizer(sequences, padding=True)
12
out = model(torch.tensor(tokens['input_ids']))
13
out.last_hidden_state
14
But how can I input word embeddings directly instead of tokens? That is, I have another model that generates word embeddings and I need to feed those into the model
Advertisement
Answer
Most (every?) huggingface encoder model supports that with the parameter inputs_embeds:
JavaScript
1
9
1
import torch
2
from transformers import RobertaModel
3
4
m = RobertaModel.from_pretrained("roberta-base")
5
6
my_input = torch.rand(2,5,768)
7
8
outputs = m(inputs_embeds=my_input)
9
P.S.: Don’t forget the attention mask in case this is required.