【发布时间】:2021-12-10 23:53:04
【问题描述】:
我正在尝试使用序列到序列模型进行命名实体识别。我的输出是简单的 IOB 标记,因此我只想预测每个标记 (IOB) 的 3 个标签的概率。
我正在尝试使用 HuggingFace 实现的 EncoderDecoderModel,其中 DistilBert 作为我的编码器,BertForTokenClassification 作为我的解码器。
首先,我导入我的编码器和解码器:
encoder = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
encoder.save_pretrained("Encoder")
decoder = BertForTokenClassification.from_pretrained('bert-base-uncased',
num_labels=3,
output_hidden_states=False,
output_attentions=False)
decoder.save_pretrained("Decoder")
decoder
当我检查我的解码器模型时,我可以清楚地看到 out_features=3 的线性分类层:
## sample of output:
)
(dropout): Dropout(p=0.1, inplace=False)
(classifier): Linear(in_features=768, out_features=3, bias=True)
)
但是,当我在我的 EncoderDecoderModel 中组合这两个模型时,似乎解码器被转换为不同类型的分类器 - 现在将 out_features 作为我词汇量的大小:
bert2bert = EncoderDecoderModel.from_encoder_decoder_pretrained("./Encoder","./Decoder")
bert2bert
## sample of output:
(cls): BertOnlyMLMHead(
(predictions): BertLMPredictionHead(
(transform): BertPredictionHeadTransform(
(dense): Linear(in_features=768, out_features=768, bias=True)
(LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
)
(decoder): Linear(in_features=768, out_features=30522, bias=True)
)
)
这是为什么呢?我怎样才能在我的模型中保留 out_features = 3?
【问题讨论】:
标签: python pytorch huggingface-transformers