# Batch\_size, seq\_length = input\_shape ValueError: too many values to unpack (expected 2) Transformer Sentence Similarity Classification

**URL:** <https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245>\
**Category:** 🤗Transformers\
**Created:** [March 6, 2024, 5:09pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245 "2024-03-06T17:09:45Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 6, 2024, 5:09pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/1 "2024-03-06T17:09:45Z")

</div>

I encountering a problem when doing Sentence Similarity Classification Task Using Transformer.

It happens when I was about to train my model, I load and process my dataset in this way, and this is what the training dataset element shape looks like:

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/4/7/47f4d5bf6ceffd48aa97a1a2cc10aba56738c59f.png)

but when I train the model, it says:

ValueError: Caught ValueError in replica 0 on device 0.  
Original Traceback (most recent call last):  
File “/home/fanshaoqi/anaconda3/envs/tf2/lib/python3.10/site-packages/torch/nn/parallel/parallel\_apply.py”, line 85, in \_worker  
output = module(\*input, \*\*kwargs)  
File “/home/fanshaoqi/anaconda3/envs/tf2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1518, in \_wrapped\_call\_impl  
return self.\_call\_impl(\*args, \*\*kwargs)  
File “/home/fanshaoqi/anaconda3/envs/tf2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1527, in \_call\_impl  
return forward\_call(\*args, \*\*kwargs)  
File “/home/fanshaoqi/anaconda3/envs/tf2/lib/python3.10/site-packages/transformers/models/bert/modeling\_bert.py”, line 1564, in forward  
outputs = self.bert(  
File “/home/fanshaoqi/anaconda3/envs/tf2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1518, in \_wrapped\_call\_impl  
return self.\_call\_impl(\*args, \*\*kwargs)  
File “/home/fanshaoqi/anaconda3/envs/tf2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1527, in \_call\_impl  
return forward\_call(\*args, \*\*kwargs)  
File “/home/fanshaoqi/anaconda3/envs/tf2/lib/python3.10/site-packages/transformers/models/bert/modeling\_bert.py”, line 967, in forward  
batch\_size, seq\_length = input\_shape  
ValueError: too many values to unpack (expected 2)

So I try this to decrease the dimension  
the training element looks like this now:  
{‘input\_ids’: \<tf.Tensor: shape=(64,), dtype=int64, numpy=  
array([ 101, 2769, 4761, 6887, 8024, 852, 3221, 800, 812, 3680, 2399,  
6963, 833, 2214, 6407, 8024, 3680, 2399, 6963, 833, 6158, 2837,  
2461, 8024, 6821, 1922, 2694, 6111, 749, 8024, 1728, 711, 800,  
812, 6375, 872, 2828, 102, 1315, 886, 800, 812, 1038, 6387,  
872, 2828, 2124, 2372, 6822, 1343, 8024, 800, 812, 3297, 5303,  
6820, 3221, 833, 6158, 2803, 1139, 1343, 511, 102])\>,  
‘token\_type\_ids’: \<tf.Tensor: shape=(64,), dtype=int64, numpy=  
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,  
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])\>,  
‘attention\_mask’: \<tf.Tensor: shape=(64,), dtype=int64, numpy=  
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])\>,  
‘labels’: \<tf.Tensor: shape=(), dtype=int64, numpy=1\>}

and the model was able to train, but its extremely slow!!!

And because this is a sentence similarity classification task, so i suppose the batch\_size should be 2? but it always 1. what it should be like?

Dont really understand why, Been struggling for a long time, Someone pls help. Thanks!

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 6, 2024, 5:10pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/2 "2024-03-06T17:10:21Z")

</div>

![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/6/0/605936cd63b6b86911b1f3c0b7f522fed7e04831.png)  
This is how i process the dataset

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 6, 2024, 5:11pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/3 "2024-03-06T17:11:26Z")

</div>

I think the first training element shape is correct, because thats what official document presents, but it just couldnt run the model.

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/5/2/52dbcc25eb4ab756f7daa3dd215052c7facedd2d.png)

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 6, 2024, 5:12pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/4 "2024-03-06T17:12:10Z")

</div>

![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/d/d/dd0014e9843c71d151187cb6373bd0ba26589519.png)  
Sorry i can only embed one image cause i’m new user

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 6, 2024, 5:12pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/5 "2024-03-06T17:12:42Z")

</div>

![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/4/c/4cb8952e02e478d53c9372cc7c5166789c314e81.png)

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 6, 2024, 5:13pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/6 "2024-03-06T17:13:15Z")

</div>

![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/4/e/4e0179bf891acc38f7071c60f2e904b74736b261.png)  
This is how i config the model

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 6, 2024, 5:13pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/7 "2024-03-06T17:13:59Z")

</div>

![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/8/d/8d1d2fe1d769870d858a01f5abb92be6b1ee36f8.png)  
training time is too long

---

<div class="post-metadata">

**Author:** ![Sandy1857](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sandy1857](https://discuss.huggingface.co/u/Sandy1857)\
**Post date:** [March 6, 2024, 11:15pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/8 "2024-03-06T23:15:57Z")

</div>

Are you using GPU?

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 7, 2024, 6:21am UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/9 "2024-03-07T06:21:27Z")

</div>

yes, it A100 with 40G memory, is this the problem?

---

<div class="post-metadata">

**Author:** ![Sandy1857](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sandy1857](https://discuss.huggingface.co/u/Sandy1857)\
**Post date:** [March 7, 2024, 3:35pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/10 "2024-03-07T15:35:27Z")

</div>

Just that I can’t see where you have loaded the model to cuda.

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 7, 2024, 5:08pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/13 "2024-03-07T17:08:01Z")

</div>

Wait is this the reason that it runs so slow? This is how I load the model.

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/b/9/b915e0f473e5cbd98e2b17f11372c06284589ddd.png)

I need to check on this, I thought it would automatically employ the model on the cuda devices when I load it. Thanks.

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 7, 2024, 5:10pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/14 "2024-03-07T17:10:50Z")

</div>

And this is what nvidia-smi shows when i already load the model. It seems it didnt run on gpu.

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/6/f/6f7656b3d6138ffb880829ce9d8e835a43ac4188.png)

I dont understand, when I run the program on my own server, when i load the model, nvidia-smi shows gpu already has it.

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 7, 2024, 5:13pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/15 "2024-03-07T17:13:29Z")

</div>

wait it wasnt, I need to load the model on cuda devices first, Thanks a lot!!! Really appreciate that!!

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 7, 2024, 5:26pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/16 "2024-03-07T17:26:25Z")

</div>

Man thanks it worked out, now it runs in a correct speed, but the very first question still confuse me, about the data shape problem, This is run in a incorrect way, the input\_id shapes should be (batch\_size, seq\_length), but it just kept telling me too many values to unpack, so i can only squeeze it to run in a wrong way, do you have any idea about this? Big Thanks!!!

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/b/9/b95c3c391f2e8dbc43c806582d050a43989d0b66.png)

---

<div class="post-metadata">

**Author:** ![Sandy1857](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sandy1857](https://discuss.huggingface.co/u/Sandy1857)\
**Post date:** [March 7, 2024, 7:03pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/17 "2024-03-07T19:03:26Z")

</div>

Try to print input shape and you will know why you got the value error. Too many values to unpack occurs when say your list has 3 items but you give only 1 or 2 variables to unpack.

Eg. my\_list = [‘item1’, ‘item2’, ‘item3’]

Var1, var2 = my\_list. Gives too many values to unpack error.

As regards the batch size, it’s not 2 just because there are two examples to compare. Batch is the number of examples you want to process/compute parallelly, in this case compute similarity. It can be 2 or 4 or 64 as is in your training arguments, to the extent your GPU cpu can handle.

---

<div class="post-metadata">

**Author:** ![caicaifan](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@caicaifan](https://discuss.huggingface.co/u/caicaifan)\
**Post date:** [March 7, 2024, 9:10pm UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/18 "2024-03-07T21:10:23Z")

</div>

Really appreciate for your suggestion man, I realize where i got it wrong.  
IAlready make it all work, Thanks again, Have a great one!!! 😁 😁 😁 🫂 🫂

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/d/a/dab775c78d77cd6140cb1a7f9a0456d7f063c860.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [March 8, 2024, 9:10am UTC](https://discuss.huggingface.co/t/batch-size-seq-length-input-shape-valueerror-too-many-values-to-unpack-expected-2-transformer-sentence-similarity-classification/76245/19 "2024-03-08T09:10:57Z")

</div>

Closed on March 8, 2024, 9:10am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
