首页 > 解决方案 > 如何在 BERT 模型上添加 Sequentilal CNN 层?

问题描述

信息

我正在处理二进制分类任务,并使用变压器库中的BERT模型使用下面的自定义模型来完成它:

class BERT(nn.Module):
  def __init__(self):
    super(BERT, self).__init__()
    self.bert = BertModel.from_pretrained(BERT_PATH, return_dict=False)
    self.dropout = nn.Dropout(0.2)
    self.out = nn.Linear(768, 1)

  def forward(self, ids, mask, token_type_ids):
    outputs = self.bert(ids, attention_mask=mask,token_type_ids=token_type_ids)
    
    # Use the pooled output
    output = self.dropout(outputs[1]) 
    return self.out(output)

我在找什么?

现在我希望CNN在上面使用BERT具有以下配置的层来查看我的模型将如何执行:

self.cnn = nn.Sequential(
        nn.Conv2d(? ? ?),
        nn.ReLU(),
        nn.MaxPool2d(? ? ?)
    )

遇到的问题。

我已经尝试过,但在设置尺寸方面遇到了错误。您认为我应该在顺序模型中放置什么配置以避免调整尺寸的问题?如果您可以复制粘贴我的代码并为我提供包含正确顺序模型的最终自定义模型,我将不胜感激。

标签: pytorchconv-neural-networkbert-language-model

解决方案


推荐阅读