【问题标题】:how to get the correct embedding from Roberta transformers?如何从罗伯塔变压器中获得正确的嵌入?
【发布时间】:2023-03-23 14:55:02
【问题描述】:

我对应该使用哪个隐藏状态作为微调的 Roberta 变换器模型的输出感到困惑。

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)

根据RobertaForMaskedLM 的 Huggingface 文档:

返回一个元组:

  1. masked_lm_loss(可选)
  2. prediction_scores
  3. hidden_​​states(可选)
  4. 注意事项(可选)

通过传递配置以启用 hidden_​​states 输出,output 是 (prediction_scores, hidden_states) 的元组

我的问题是: 我应该使用output[-1][0]output[-1][-1] 作为微调罗伯塔模型的最终输出嵌入吗?我的理解是,output[-1][0] 是输入到 Roberta 模型的初始嵌入,output[-1][-1] 是最终的嵌入输出。

【问题讨论】:

    标签: bert-language-model huggingface-transformers


    【解决方案1】:

    output[-1][-1] 是正确的,如果您正在寻找最后一个编码层的输出。您可以通过查看source code 来解决这个问题,并通过比较输出来验证它:

    import torch
    
    print(len(output[-1]))
    
    outputEmbeddings = model.roberta.embeddings(sentence)
    
    #the first tensor is the output of the embedding layer
    print(torch.equal(output[1][0],  outputEmbeddings))
    
    #the second tensor is the output of the first encoding layer
    print(torch.equal(output[1][1], model.roberta.encoder.layer[0](outputEmbeddings)[0]))
    
    previousLayer = outputEmbeddings
    for x in range(12):
        #it is now the current layer
        previousLayer = model.roberta.encoder.layer[x](previousLayer)[0]
        print(torch.equal(output[1][1+x], previousLayer))
    

    输出:

    13
    True
    True
    True
    True
    True
    True
    True
    True
    True
    True
    True
    True
    True
    True
    

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 2022-10-22
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多