python - bert 模型中的张量类型属性以字符串形式返回
问题描述
我是 nlp 的新手,我想为情绪分析建立一个 bert 模型,所以我正在关注这个教程 https://curiousily.com/posts/sentiment-analysis-with-bert-and-hugging-face-using-pytorch- and-python/ 但我收到以下错误
bert_model = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME)
last_hidden_state, pooled_output = bert_model(
input_ids=encoding['input_ids'],
attention_mask=encoding['attention_mask']
)
last_hidden_state.shape
pooled_output.shape
当我想执行 last_hidden_state.shape 我得到一个错误:
'str' 对象没有属性 'shape' 为什么它返回 last_hidden_state 和 pooled_output 作为 str 而不是张量。谢谢你。
解决方案
从版本 3 到版本 4 的切换是在拥抱脸完成时引入了一些更改,可以像下面这样解决
bert_model = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME, return_dict=False)