Home > database >  Pytorch Tensor Error: ValueError: Expected input batch_size (1) to match target batch_size (2)
Pytorch Tensor Error: ValueError: Expected input batch_size (1) to match target batch_size (2)

Time:02-05

I am currently creating a convolutional neural network for a rock, paper, scissors program and I get the following error: ValueError: Expected input batch_size (1) to match target batch_size (2).

When I print the size of "target", I get torch.size([1,0]) and the size of "input" is of torch.Size([1, 3, 150, 150]).

This code trains the convolutional neural network model I currently have. The first function "img_configs", turns the image into readable data. Then the function "train_bath_craetor" gives me random images that I can use for training.

Before this error, I had a problem where my code was predictable for the training. The thing was that there was a pattern. The training data always repeated "paper", "rock", "scissors" values. So I had to figure out a way where I can shuffle the input list so I shuffled the list. But then I also have to know which tensor has the correct label (correct value). So I applied this code:

temp = list(zip(inputs, outputs))

random.shuffle(temp)

res1, res2 = zip(*temp)

shuffled_inputs, shuffled_outputs = list(res1), list(res2)

This code shuffles both lists the exact same way so I now know which input has the correct label attached to it. Paper has a label of 0, rock has 1, scissors has 2.

CodePudding user response:

I don't think I could guess why your batch sizes aren't matching up without seeing what your training loop looks like, but if you started getting this error after implementing your shuffling code, you could try replacing it with a PyTorch DataLoader, which is the standard way of shuffling and batching training data in PyTorch.

For example if I have an ordered dataset and I want to load batches of 3 random examples at a time:

from torch.utils.data import DataLoader

dataset = [
    {"input": "input0", "target": "rock"},
    {"input": "input1", "target": "paper"},
    {"input": "input2", "target": "scissors"},
    {"input": "input3", "target": "rock"},
    {"input": "input4", "target": "paper"},
    {"input": "input5", "target": "scissors"},
    {"input": "input6", "target": "rock"},
    {"input": "input7", "target": "paper"},
    {"input": "input8", "target": "scissors"},
]

dataloader = DataLoader(dataset, batch_size=3, shuffle=True)

for batch in dataloader:
    print(batch)

Output:

{'input': ['input0', 'input1', 'input2'], 'target': ['rock', 'paper', 'scissors']}
{'input': ['input4', 'input7', 'input3'], 'target': ['paper', 'paper', 'rock']}
{'input': ['input6', 'input8', 'input5'], 'target': ['rock', 'scissors', 'scissors']}
  • Related