[HELP] How do I solve this error : Expected input batch_size (1140) to match target batch_size (632)
![](http://web.archive.org./web/20230512095909im_/https://www.redditstatic.com/desktop2x/img/renderTimingPixel.png)
I am trying to train GPT2 model on a task as in this notebook : Colab notebook
I am getting the following error :
ValueError: Expected input batch_size (1140) to match target batch_size (632).
The relevant code is as follows :
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = torch.device('cpu')
for epoch in range(max_epochs): model.train() total_loss = 0 batch_count = 0
for batch_input, batch_output in train_loader:
batch_size_actual = batch_input.size(0)
# Check if the batch sizes are different and skip the incomplete batch
if batch_size_actual != batch_size:
continue
batch_input = batch_input.to(device)
batch_output = batch_output.to(device)
About Community
r/learnpython Rules
Related Subreddits
![](http://web.archive.org./web/20230512095909im_/https://styles.redditmedia.com/t5_2qh0y/styles/communityIcon_h9cdwd9m75a51.png?width=256&v=enabled&s=cefd3a42f994dcee5281520c6e4c2baabc4d9ae1)
1,126,821 members
![](http://web.archive.org./web/20230512095909im_/https://styles.redditmedia.com/t5_2qh4v/styles/communityIcon_r1rcce3bp1241.png?width=256&v=enabled&s=bde7c360a985d6271858e189da19d03671c51213)
111,077 members
![](http://web.archive.org./web/20230512095909im_/https://styles.redditmedia.com/t5_2s1s3/styles/communityIcon_o4vkby94vkz41.png?width=256&v=enabled&s=009ae27a22252c14b7263d2c52bc0f974dd6937b)
78,874 members
![](http://web.archive.org./web/20230512095909im_/https://styles.redditmedia.com/t5_2fwo/styles/communityIcon_1bqa1ibfp8q11.png?width=256&v=enabled&s=f1dfc46cdf4e7c38085fbc95f20af558e1a0fd65)
5,409,178 members
3,816,149 members
237,011 members
Commenting Guidelines
Try to guide OP to a solution instead of providing one directly.
Provide links to related resources.
Answer the question and highlight side-issues if any exist.
Don't "answer and run", be prepared to respond to follow up questions.
Proofread your answers for clarity and correctness.
Be polite.
Posting Guidelines
Try out suggestions you get and report back.
SSCCE Keep your code Short, Self Contained, Correct (Compilable) and provide Example
Include the error you get when running the code, if there is one.
Ensure your example is correct. Either the example compiles cleanly, or causes the exact error message about which you want help.
Avoid posting a lot of code in your posts.
Posting homework assignments is not prohibited if you show that you tried to solve it yourself.