# ValueError: too many values to unpack (expected 2) in text summarization. Possibly due to nested lists?

**URL:** <https://discuss.huggingface.co/t/valueerror-too-many-values-to-unpack-expected-2-in-text-summarization-possibly-due-to-nested-lists/16766>\
**Category:** 🤗Transformers\
**Created:** [April 12, 2022, 8:54pm UTC](https://discuss.huggingface.co/t/valueerror-too-many-values-to-unpack-expected-2-in-text-summarization-possibly-due-to-nested-lists/16766 "2022-04-12T20:54:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cormorant](https://avatars.discourse-cdn.com/v4/letter/c/a6a055/32.png) [@cormorant](https://discuss.huggingface.co/u/cormorant)\
**Post date:** [April 12, 2022, 8:54pm UTC](https://discuss.huggingface.co/t/valueerror-too-many-values-to-unpack-expected-2-in-text-summarization-possibly-due-to-nested-lists/16766/1 "2022-04-12T20:54:30Z")

</div>

Hi, I am getting this error when executing Trainer.train()

All of my code works successfully when I am not using the return\_tensors=“pt”).to(device) code during tokenizing.  
I wanted to utilize my gpu which is why I changed my code. However, now I end up with the error ValueError: too many values to unpack (expected 2).

```auto
path = "/content/drive/MyDrive/pickle_files/pfizer_data.pkl"

df = pd.read_pickle(path) 
pfizer = Dataset.from_pandas(df)

test_size=0.33

pfizer = pfizer.shuffle().train_test_split(test_size=test_size)
pfizer

```

> DatasetDict({  
> train: Dataset({  
> features: [‘text’, ‘summary’],  
> num\_rows: 40  
> })  
> test: Dataset({  
> features: [‘text’, ‘summary’],  
> num\_rows: 20  
> })  
> })

```auto
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("google/pegasus-xsum")

```

```auto
def preprocess_function(examples):
    model_inputs = tokenizer(examples["text"], max_length=512, truncation=True, padding="max_length", return_tensors="pt").to(device)
    # Set up the tokenizer for targets
    with tokenizer.as_target_tokenizer():
        labels = tokenizer(examples["summary"], max_length=512, truncation=True, padding="max_length", return_tensors="pt").to(device)
    model_inputs["labels"] = labels["input_ids"]
    return model_inputs

tokenized_datasets = pfizer.map(preprocess_function)

```

```auto
from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer

model = AutoModelForSeq2SeqLM.from_pretrained("google/pegasus-xsum")
model = model.to(device)

from transformers import TrainingArguments

training_args = Seq2SeqTrainingArguments(output_dir="test_trainer")

```

```auto
from transformers import TrainingArguments, Trainer
trainer = Seq2SeqTrainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_datasets["train"],
    eval_dataset=tokenized_datasets["test"]
)

```

Then when I run `trainer.train()` I get the error ValueError: too many values to unpack (expected 2).

I have compared the data set in each step to the data set when not using return\_tensors=“pt” or to(device) functions, and it is only different in the amount of nested lists.

For example in my successful run my data set structure is as follows

```auto
tokenized_datasets["train"]["input_ids"]

```

> [[2920,  
> 8478,  
> 30156,  
> …  
> 5049,  
> 1096,  
> 18149,  
> 112,  
> 1008,  
> 1],  
> [2920,  
> 8478,  
> 30156,  
> 8233,  
> 8478,  
> etc

Whereas it is as follows when I get the error

```auto
tokenized_datasets["train"]["input_ids"]

```

> [[[2920,  
> 8478,  
> 30156,  
> …  
> 5049,  
> 1096,  
> 18149,  
> 112,  
> 1008,  
> 1]],  
> [[2920,  
> 8478,  
> 30156,  
> 8233,  
> 8478,  
> etc

So I am thinking the error might be due to the nested lists? If yes, how can I get rid of it or what is causing the nested lists?

I have tried things like

```auto
flat_list = [item for sublist in tokenized_datasets["train"]["input_ids"] for item in sublist]
tokenized_datasets["train"]["input_ids"] = flat_list

```

But that does not work and I get an TypeError: ‘Dataset’ object does not support item assignment.

Thank you

my whole error stack is as follows

> ----\> 1 trainer.train()
> 
> 9 frames  
> /usr/local/lib/python3.7/dist-packages/transformers/trainer.py in train(self, resume\_from\_checkpoint, trial, ignore\_keys\_for\_eval, \*\*kwargs)  
> 1420 tr\_loss\_step = self.training\_step(model, inputs)  
> 1421 else:  
> → 1422 tr\_loss\_step = self.training\_step(model, inputs)  
> 1423  
> 1424 if (
> 
> /usr/local/lib/python3.7/dist-packages/transformers/trainer.py in training\_step(self, model, inputs)  
> 2009  
> 2010 with self.autocast\_smart\_context\_manager():  
> → 2011 loss = self.compute\_loss(model, inputs)  
> 2012  
> 2013 if self.args.n\_gpu \> 1:
> 
> /usr/local/lib/python3.7/dist-packages/transformers/trainer.py in compute\_loss(self, model, inputs, return\_outputs)  
> 2041 else:  
> 2042 labels = None  
> → 2043 outputs = model(\*\*inputs)  
> 2044 # Save past state if it exists  
> 2045 # TODO: this needs to be fixed and made cleaner later.
> 
> /usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in \_call\_impl(self, \*input, \*\*kwargs)  
> 1100 if not (self.\_backward\_hooks or self.\_forward\_hooks or self.\_forward\_pre\_hooks or \_global\_backward\_hooks  
> 1101 or \_global\_forward\_hooks or \_global\_forward\_pre\_hooks):  
> → 1102 return forward\_call(\*input, \*\*kwargs)  
> 1103 # Do not call functions when jit is used  
> 1104 full\_backward\_hooks, non\_full\_backward\_hooks = ,
> 
> /usr/local/lib/python3.7/dist-packages/transformers/models/pegasus/modeling\_pegasus.py in forward(self, input\_ids, attention\_mask, decoder\_input\_ids, decoder\_attention\_mask, head\_mask, decoder\_head\_mask, cross\_attn\_head\_mask, encoder\_outputs, past\_key\_values, inputs\_embeds, decoder\_inputs\_embeds, labels, use\_cache, output\_attentions, output\_hidden\_states, return\_dict)  
> 1405 output\_attentions=output\_attentions,  
> 1406 output\_hidden\_states=output\_hidden\_states,  
> → 1407 return\_dict=return\_dict,  
> 1408 )  
> 1409 lm\_logits = self.lm\_head(outputs[0]) + self.final\_logits\_bias
> 
> /usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in \_call\_impl(self, \*input, \*\*kwargs)  
> 1100 if not (self.\_backward\_hooks or self.\_forward\_hooks or self.\_forward\_pre\_hooks or \_global\_backward\_hooks  
> 1101 or \_global\_forward\_hooks or \_global\_forward\_pre\_hooks):  
> → 1102 return forward\_call(\*input, \*\*kwargs)  
> 1103 # Do not call functions when jit is used  
> 1104 full\_backward\_hooks, non\_full\_backward\_hooks = ,
> 
> /usr/local/lib/python3.7/dist-packages/transformers/models/pegasus/modeling\_pegasus.py in forward(self, input\_ids, attention\_mask, decoder\_input\_ids, decoder\_attention\_mask, head\_mask, decoder\_head\_mask, cross\_attn\_head\_mask, encoder\_outputs, past\_key\_values, inputs\_embeds, decoder\_inputs\_embeds, use\_cache, output\_attentions, output\_hidden\_states, return\_dict)  
> 1236 output\_attentions=output\_attentions,  
> 1237 output\_hidden\_states=output\_hidden\_states,  
> → 1238 return\_dict=return\_dict,  
> 1239 )  
> 1240 # If the user passed a tuple for encoder\_outputs, we wrap it in a BaseModelOutput when return\_dict=True
> 
> /usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in \_call\_impl(self, \*input, \*\*kwargs)  
> 1100 if not (self.\_backward\_hooks or self.\_forward\_hooks or self.\_forward\_pre\_hooks or \_global\_backward\_hooks  
> 1101 or \_global\_forward\_hooks or \_global\_forward\_pre\_hooks):  
> → 1102 return forward\_call(\*input, \*\*kwargs)  
> 1103 # Do not call functions when jit is used  
> 1104 full\_backward\_hooks, non\_full\_backward\_hooks = ,
> 
> /usr/local/lib/python3.7/dist-packages/transformers/models/pegasus/modeling\_pegasus.py in forward(self, input\_ids, attention\_mask, head\_mask, inputs\_embeds, output\_attentions, output\_hidden\_states, return\_dict)  
> 767 if attention\_mask is not None:  
> 768 # [bsz, seq\_len] → [bsz, 1, tgt\_seq\_len, src\_seq\_len]  
> → 769 attention\_mask = \_expand\_mask(attention\_mask, inputs\_embeds.dtype)  
> 770  
> 771 encoder\_states = () if output\_hidden\_states else None
> 
> /usr/local/lib/python3.7/dist-packages/transformers/models/pegasus/modeling\_pegasus.py in \_expand\_mask(mask, dtype, tgt\_len)  
> 96 Expands attention\_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.  
> 97 “”"  
> —\> 98 bsz, src\_len = mask.size()  
> 99 tgt\_len = tgt\_len if tgt\_len is not None else src\_len  
> 100
> 
> ValueError: too many values to unpack (expected 2)

---

<div class="post-metadata">

**Author:** ![Indroganguly](https://avatars.discourse-cdn.com/v4/letter/i/ccd318/32.png) [@Indroganguly](https://discuss.huggingface.co/u/Indroganguly)\
**Post date:** [September 29, 2023, 7:52pm UTC](https://discuss.huggingface.co/t/valueerror-too-many-values-to-unpack-expected-2-in-text-summarization-possibly-due-to-nested-lists/16766/2 "2023-09-29T19:52:54Z")

</div>

> [@cormorant](#):
>
> `to(device)`

Hi, did you found the solution to this i am facing similar issue with Flan T5
