I've tried to add custom layers to HuggingFace Transformer model on binary classification task. As an absolute beginner, I tried to follow this tutorial
Here's the custom model
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)
Unfortunately, it generated an error:
<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
Also, you can find all of the code here.