pytorch 如何访问huggingface中代码生成模型的“last_hidden_state”?

lmyy7pcs  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(353)

我试图通过here获取代码生成模型的“last_hidden_state”(如here所述)。我无法确定如何继续,只能手动下载每个代码生成模型,并使用以下代码检查其键是否具有该属性-

import numpy as np
from datasets import load_dataset
from transformers import AutoTokenizer
from transformers import AutoModel, AutoModelForCausalLM
import torch
from sklearn.linear_model import LogisticRegression

from transformers import AutoTokenizer, AutoModelWithLMHead

tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AutoModelWithLMHead.from_pretrained("codeparrot/codeparrot").to(device)

inputs = tokenizer("def hello_world():", return_tensors="pt")
inputs = {k:v.to(device) for k,v in inputs.items()}
with torch.no_grad():
    outputs = model(**inputs)
print(outputs.keys())

到目前为止,我在CodeParrot和InCoder上尝试了这种策略,但没有成功。也许有更好的方法来访问隐藏层的值?

ma8fv8wu

ma8fv8wu1#

CodeGenForCausalLM输出的hidden_states已经是codegen模型的last_hidden_state。请参阅:连结
其中hidden_states = transformer_outputs[0]CodeGenModel(链路)的输出,transformer_outputs[0]last_hidden_state

if not return_dict:
            return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None)

        return BaseModelOutputWithPast(
            last_hidden_state=hidden_states,
            past_key_values=presents,
            hidden_states=all_hidden_states,
            attentions=all_self_attentions,
        )

相关问题