Ale*_*lex 2 python transformer-model bert-language-model huggingface-transformers
我只是想知道是否可以将 HuggingFace BertForSequenceClassification模型扩展到 2 个以上的标签。文档说,我们可以传递位置参数,但似乎“标签”不起作用。有人有想法吗?
labels = th.tensor([0,0,0,0,0,0], dtype=th.long).unsqueeze(0)
print(labels.shape)
modelBERTClass = transformers.BertForSequenceClassification.from_pretrained(
'bert-base-uncased',
labels=labels
)
l = [module for module in modelBERTClass.modules()]
l
Run Code Online (Sandbox Code Playgroud)
torch.Size([1, 6])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-122-fea9a36402a6> in <module>()
3 modelBERTClass = transformers.BertForSequenceClassification.from_pretrained(
4 'bert-base-uncased',
----> 5 labels=labels
6 )
7
/usr/local/lib/python3.6/dist-packages/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
653
654 # Instantiate model.
--> 655 model = cls(config, *model_args, **model_kwargs)
656
657 if state_dict is None and not from_tf:
TypeError: __init__() got an unexpected keyword argument 'labels'
Run Code Online (Sandbox Code Playgroud)
from_pretrained
您可以通过参数设置分类层的输出形状num_labels
:
from transformers import BertForSequenceClassification
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=6)
print(model.classifier.parameters)
Run Code Online (Sandbox Code Playgroud)
输出:
Linear(in_features=768, out_features=6, bias=True)
Run Code Online (Sandbox Code Playgroud)