I got confused by which hidden state should I use as the output of fine-tuned Roberta transformer models.
from transformers import AutoConfig, AutoModelForMaskedLM, AutoTokenizer
config = AutoConfig.from_pretrained("roberta-base")
config.output_hidden_states = True
tok = AutoTokenizer.from_pretrained("roberta-base")
model = AutoModelForMaskedLM.from_pretrained("roberta-base", config=config)
inp = "alright let s do this "
sentence = tok.encode(inp, padding='max_length', max_length=512, truncation=True, return_tensors='pt')
output = model(sentence)
According to Huggingface documentation for RobertaForMaskedLM:
Returns a tuple of:
- masked_lm_loss (optional)
- prediction_scores
- hidden_states (optional)
- attentions (optional)
By passing the config to enable hidden_states output, the output is a tuple of (prediction_scores, hidden_states)
My question is:
should I use output[-1][0] or output[-1][-1] as the final output embedding from the fine-tuned Roberta Model? My understanding is that output[-1][0] is the initial embedding feeding into the Roberta Model, and output[-1][-1] is the final embedding output.