Too many values to unpack (expected 2) while working with sequence classification

Hello everyone,

I am working on sequence classification where I have created my own Dataset class and using that to train.

Here is the dataset class:

class BERTDataset:
    def __init__(self, input_ids, attention_masks, token_type_ids, targets):
        self.input_ids = input_ids
        self.attention_masks = attention_masks
        self.token_type_ids = token_type_ids
        self.targets = targets
    def __getitem__(self, index):
        ids = torch.tensor(self.input_ids, dtype=torch.long)
        masks = torch.tensor(self.attention_masks, dtype=torch.long)
        token_type_ids = torch.tensor(self.token_type_ids, dtype=torch.long)
        targets = torch.tensor(self.targets, dtype=torch.long)
        return {
            'ids': ids,
            'mask': masks,
            'token_type_ids': token_type_ids,
            'targets': self.targets[index]
    def __len__(self,):
        return len(self.input_ids)

The way I fetching for train is as follows:


y_preds = []

for epoch in range(epochs):
    for i, sample in enumerate(train_dataloader):
        ids = sample["ids"]
        masks = sample["mask"]
        token_type_ids = sample["token_type_ids"]
        target = sample["targets"]
        ids =, dtype=torch.long)
        masks =, dtype=torch.long)
        token_type_ids =, dtype=torch.long)
        target =, dtype=torch.long)
        y_pred = model(tokens,

I am getting the following error: too many values to unpack (expected 2)

This is for the last line where I am calculating y_pred from model() object.

What am I missing here?

Based of your description of the issue I assume you are trying to unpack some objects inside the forward method of your model, which is failing. If that’s the case, check the complete stacktrace and make sure the objects, which should be unpacked contains enough values.

This is my model:

class BERTBert(nn.Module):
    def __init__(self):
        super(BERTBert, self).__init__()
        self.bert = transformers.BertModel.from_pretrained('bert-base-uncased')
        self.drop = nn.Dropout(0.3)
        self.out = nn.Linear(768, 1)
    def forward(self, ids, masks, token_type_ids):
        output = self.bert(ids,
        output = self.drop(output)
        output = self.out(output)
        return output