Given a sentence of the type 'Roberta is a heavily optimized version of BERT.', I need to get the embeddings for each of the words in this sentence with RoBERTa. I have tried to look at the sample codes online, failing to find a definite answer.
My take is the following:
tokens = roberta.encode(headline)
all_layers = roberta.extract_features(tokens, return_all_hiddens=True)
embedding = all_layers[0]
n = embedding.size()[1] - 1
embedding = embedding[:,1:n,:]
where embedding[:,1:n,:]
is used to extract only the embeddings for the words in the sentence, without the start and end tokens.
Is it correct?
bert
is unused, but you can usehuggingface-transformers
instead). Since there are several implementations, it is otherwise hard to termine the right implementation and give a correct answer. – Thuriferembedding = all_layers[-1][-1]
– Apostate