IndexError:张量的索引太多2:在huggingface模型上添加自定义层时
我尝试在二进制分类任务上将自定义层添加到HuggingFace Transformer模型中。作为一个绝对的初学者,我试图跟随此教程
不幸的是,这是一个自定义模型
class CustomModel(nn.Module):
def __init__(self,checkpoint,num_labels):
super(CustomModel,self).__init__()
self.num_labels = num_labels
#Load Model with given checkpoint and extract its body
self.model = AutoModelForSequenceClassification.from_pretrained(checkpoint,config=AutoConfig.from_pretrained(checkpoint, output_attentions=True,output_hidden_states=True))
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(768,num_labels) # load and initialize weights
def forward(self, input_ids=None, attention_mask=None,labels=None):
#Extract outputs from the body
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask)
#Add custom layers
sequence_output = self.dropout(outputs[0]) #outputs[0]=last hidden state
logits = self.classifier(sequence_output[:,0,:].view(-1,768)) # calculate losses
loss = None
if labels is not None:
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
return TokenClassifierOutput(loss=loss, logits=logits, hidden_states=outputs.hidden_states,attentions=outputs.attentions)
,它产生了一个错误:
<ipython-input-32-5d5e07952b71> in forward(self, input_ids, attention_mask, labels)
19 sequence_output = self.dropout(outputs[0]) #outputs[0]=last hidden state
20
---> 21 logits = self.classifier(sequence_output[:,0,:].view(-1,768)) # calculate losses
22
23 loss = None
IndexError: too many indices for tensor of dimension 2
另外,您可以找到所有代码此处。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论