如何将微调的 bert 模型的输出作为输入提供给另一个微调的 bert 模型?
How to feed the output of a finetuned bert model as inpunt to another finetuned bert model?
我在情感分析和 pos 标记任务上微调了两个独立的 bert 模型 (bert-base-uncased)。现在,我想将 pos 标记器的输出(批次、seqlength、隐藏大小)作为输入输入到情绪 model.The 原始 bert-base-uncased 模型位于 'bertModel/' 文件夹中,其中包含 'model.bin' 和 'config.json'。这是我的代码:
class DeepSequentialModel(nn.Module):
def __init__(self, sentiment_model_file, postag_model_file, device):
super(DeepSequentialModel, self).__init__()
self.sentiment_model = SentimentModel().to(device)
self.sentiment_model.load_state_dict(torch.load(sentiment_model_file, map_location=device))
self.postag_model = PosTagModel().to(device)
self.postag_model.load_state_dict(torch.load(postag_model_file, map_location=device))
self.classificationLayer = nn.Linear(768, 1)
def forward(self, seq, attn_masks):
postag_context = self.postag_model(seq, attn_masks)
sent_context = self.sentiment_model(postag_context, attn_masks)
logits = self.classificationLayer(sent_context)
return logits
class PosTagModel(nn.Module):
def __init__(self,):
super(PosTagModel, self).__init__()
self.bert_layer = BertModel.from_pretrained('bertModel/')
self.classificationLayer = nn.Linear(768, 43)
def forward(self, seq, attn_masks):
cont_reps, _ = self.bert_layer(seq, attention_mask=attn_masks)
return cont_reps
class SentimentModel(nn.Module):
def __init__(self,):
super(SentimentModel, self).__init__()
self.bert_layer = BertModel.from_pretrained('bertModel/')
self.cls_layer = nn.Linear(768, 1)
def forward(self, input, attn_masks):
cont_reps, _ = self.bert_layer(encoder_hidden_states=input, encoder_attention_mask=attn_masks)
cls_rep = cont_reps[:, 0]
return cls_rep
但是我收到以下错误。如果有人可以帮助我,我将不胜感激。谢谢!
cont_reps, _ = self.bert_layer(encoder_hidden_states=input, encoder_attention_mask=attn_masks)
result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument 'encoder_hidden_states'
为了将此也表述为答案,并使其对未来的访问者正确可见,forward()
调用变形金刚 does not support these arguments in version 2.1.1,或任何更早的版本,就此而言。请注意,我评论中的 link 实际上指向不同的前向函数,但除此之外,这一点仍然成立。
将 encoder_hidden_states
传递给 forward()
是 first possible in version 2.2.0。
我在情感分析和 pos 标记任务上微调了两个独立的 bert 模型 (bert-base-uncased)。现在,我想将 pos 标记器的输出(批次、seqlength、隐藏大小)作为输入输入到情绪 model.The 原始 bert-base-uncased 模型位于 'bertModel/' 文件夹中,其中包含 'model.bin' 和 'config.json'。这是我的代码:
class DeepSequentialModel(nn.Module):
def __init__(self, sentiment_model_file, postag_model_file, device):
super(DeepSequentialModel, self).__init__()
self.sentiment_model = SentimentModel().to(device)
self.sentiment_model.load_state_dict(torch.load(sentiment_model_file, map_location=device))
self.postag_model = PosTagModel().to(device)
self.postag_model.load_state_dict(torch.load(postag_model_file, map_location=device))
self.classificationLayer = nn.Linear(768, 1)
def forward(self, seq, attn_masks):
postag_context = self.postag_model(seq, attn_masks)
sent_context = self.sentiment_model(postag_context, attn_masks)
logits = self.classificationLayer(sent_context)
return logits
class PosTagModel(nn.Module):
def __init__(self,):
super(PosTagModel, self).__init__()
self.bert_layer = BertModel.from_pretrained('bertModel/')
self.classificationLayer = nn.Linear(768, 43)
def forward(self, seq, attn_masks):
cont_reps, _ = self.bert_layer(seq, attention_mask=attn_masks)
return cont_reps
class SentimentModel(nn.Module):
def __init__(self,):
super(SentimentModel, self).__init__()
self.bert_layer = BertModel.from_pretrained('bertModel/')
self.cls_layer = nn.Linear(768, 1)
def forward(self, input, attn_masks):
cont_reps, _ = self.bert_layer(encoder_hidden_states=input, encoder_attention_mask=attn_masks)
cls_rep = cont_reps[:, 0]
return cls_rep
但是我收到以下错误。如果有人可以帮助我,我将不胜感激。谢谢!
cont_reps, _ = self.bert_layer(encoder_hidden_states=input, encoder_attention_mask=attn_masks)
result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument 'encoder_hidden_states'
为了将此也表述为答案,并使其对未来的访问者正确可见,forward()
调用变形金刚 does not support these arguments in version 2.1.1,或任何更早的版本,就此而言。请注意,我评论中的 link 实际上指向不同的前向函数,但除此之外,这一点仍然成立。
将 encoder_hidden_states
传递给 forward()
是 first possible in version 2.2.0。