【发布时间】:2021-06-06 00:32:52
【问题描述】:
AttributeError: 'str' object has no attribute 'shape' 同时使用 BertModel 和 PyTorch(拥抱脸)对张量进行编码。下面是代码
bert_model = BertModel.from_pretrained(r'downloads\bert-pretrained-model')
input_ids
输出是:
tensor([[ 101, 156, 13329, ..., 0, 0, 0],
[ 101, 156, 13329, ..., 0, 0, 0],
[ 101, 1302, 1251, ..., 0, 0, 0],
...,
[ 101, 25456, 1200, ..., 0, 0, 0],
[ 101, 143, 9664, ..., 0, 0, 0],
[ 101, 2586, 7340, ..., 0, 0, 0]])
下面是代码
last_hidden_state, pooled_output = bert_model(
input_ids=encoding['input_ids'],
attention_mask=encoding['attention_mask']
)
下面是代码
last_hidden_state.shape
输出是
AttributeError Traceback (most recent call last)
<ipython-input-70-9628339f425d> in <module>
----> 1 last_hidden_state.shape
AttributeError: 'str' object has no attribute 'shape'
完整的代码链接是'https://colab.research.google.com/drive/1FY4WtqCi2CQ9RjHj4slZwtdMhwaWv2-2?usp=sharing'
【问题讨论】:
-
您能提供
bert_model函数的定义吗?还是模块中的函数? -
我已经上传了bert预训练模型,就这样
标签: python string pytorch attributeerror huggingface-transformers