Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pytorch 如何将一个微调的伯特模型的输出作为输入输入到另一个微调的伯特模型?_Pytorch_Pre Trained Model_Bert Language Model_Huggingface Transformers - Fatal编程技术网

Pytorch 如何将一个微调的伯特模型的输出作为输入输入到另一个微调的伯特模型?

Pytorch 如何将一个微调的伯特模型的输出作为输入输入到另一个微调的伯特模型?,pytorch,pre-trained-model,bert-language-model,huggingface-transformers,Pytorch,Pre Trained Model,Bert Language Model,Huggingface Transformers,我在情感分析和词性标注任务的基础上微调了两个独立的伯特模型(伯特基未基)。现在,我想将pos标记器的输出(batch、seqlength、hiddensize)作为情感模型的输入。原始的bert-base未分类模型位于“bertModel/”文件夹中,其中包含“model.bin”和“config.json”。这是我的密码: class DeepSequentialModel(nn.Module): def __init__(self, sentiment_model_file, postag_

我在情感分析和词性标注任务的基础上微调了两个独立的伯特模型(伯特基未基)。现在,我想将pos标记器的输出(batch、seqlength、hiddensize)作为情感模型的输入。原始的bert-base未分类模型位于“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'

为了将其表述为一个答案,并使其对未来的访问者保持适当的可视性,可以调用transformers的
forward()
调用,或者任何早期版本。请注意,我的评论中的链接实际上指向一个不同的转发函数,但除此之外,这一点仍然成立


encoder\u hidden\u状态
传递到
forward()
was。

您使用的是哪个版本的
transformers
?@Denninger transformers 2.1.1现在是更新您的transformers版本的时候了。显然列出了一个非常过时的向前传球版本,因此这就是您无法访问(此时不存在)参数的原因。