I have output data as follows:

enter image description here

Then I encode into :

enter image description here

Then I convert into vector:

enter image description here

The input of model is word embedding of sentence. My question is that: Can ML or DL return a vector output above? If possible, how can it calculate the performance?

  • $\begingroup$ "Return" you mean "predict"? What will be the predictor? $\endgroup$ Jul 28 at 5:29
  • 1
    $\begingroup$ I think you are looking for en.wikipedia.org/wiki/Multi-label_classification $\endgroup$
    – Akavall
    Jul 28 at 5:37
  • $\begingroup$ yes,return means predict, predictors are DS1, DS2, DS3, DS4 $\endgroup$
    – Sherry
    Jul 28 at 5:38
  • $\begingroup$ Multi - label is for one output, here I have 4 output, each out put i have 3 classes $\endgroup$
    – Sherry
    Jul 28 at 5:39
  • 1
    $\begingroup$ @Sherry I think that you're confused: multi-label is for multiple independent target variables, similarly to what you want. You can also do this by simply training 4 independent models. Also don't forget to use @user when you reply to a comment, otherwise the person doesn't receive a notification. $\endgroup$
    – Erwan
    Jul 28 at 21:22

1 Answer 1


I think you are trying to do a multilabel + multiclass classification.

You would need to do:

  1. Have multiple output layers for each multiclass classification(Each for your Ds1, Ds2, Ds3, Ds4)
  2. Calculate cross entropy loss for each one them
  3. Sum them to have your final loss to be used for backpropagation.

Below is an example (Not an optimized one)

   from torch import nn
   from transformers import AutoModel
   class CustomModel(nn.Module):   
        def __init__(self,num_labels1, num_labels2, num_labels3, num_labels4): 
            self.num_labels1 = num_labels1 # number of unique values in ds1
            self.num_labels2 = num_labels2 # number of unique values in ds2
            self.num_labels3 = num_labels3 # number of unique values in ds3
            self.num_labels4 = num_labels4 # number of unique values in ds4

            #Load Model with given checkpoint and extract its body
            self.model = AutoModel.from_pretrained("bert-base-uncased")

            self.classifier1 = nn.Linear(768,num_labels1) # output layer1
            self.classifier2 = nn.Linear(768,num_labels2) # output layer2
            self.classifier3 = nn.Linear(768,num_labels3) # output layer3
            self.classifier4 = nn.Linear(768,num_labels4) # output layer4

        def forward(self, input_ids=None, attention_mask=None, labels1=None, labels2=None, labels3=None, labels4=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

            logits1 = self.classifier1(sequence_output[:,0,:].view(-1,768)) 

            logits2 = self.classifier2(sequence_output[:,0,:].view(-1,768)) 

            logits3 = self.classifier3(sequence_output[:,0,:].view(-1,768)) 

            logits4 = self.classifier4(sequence_output[:,0,:].view(-1,768))

            loss = None
            if self.training:
                loss_fct = nn.CrossEntropyLoss()

                #compute individual losses
                loss1 = loss_fct(logits1.view(-1, self.num_labels1), labels1.view(-1))
                loss2 = loss_fct(logits2.view(-1, self.num_labels2), labels2.view(-1))
                loss3 = loss_fct(logits3.view(-1, self.num_labels3), labels3.view(-1))
                loss4 = loss_fct(logits4.view(-1, self.num_labels4), labels4.view(-1))

                #sum them up as final loss and return
                loss = loss1 + loss2 + loss3 + loss4

            # return all the logits inorder to get that vector of outputs---
            return (loss, [logits1, logits2, logits3, logits4])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.