title: How to solve error: no match between expected input batch size and target batch size in PyTorch?
body: <p>I attempting to create a logistic model on CIFAR10 dataset by PyTorch. However I am getting an error:</p>
  
  <blockquote>
    <p>ValueError: Expected input batch_size (900) to match target batch_size (300).</p>
  </blockquote>
  
  <p>What I think is happening is that 3*100 is 300. So may be the 3 axis of the RGB image is doing that but I cant figure how to solve.</p>
  
  <p>These are my hyperparameters.</p>
  
  <pre><code>batch_size = 100
  learning_rate = 0.001
  
  # Other constants
  input_size = 32*32
  num_classes = 10
  </code></pre>
  
  <p>Here I divide my data into train, validation and test data.</p>
  
  <pre><code>
  transform_train = transforms.Compose([transforms.Resize((32,32)),
                                        transforms.RandomHorizontalFlip(),
                                        transforms.RandomRotation(10),
                                        transforms.RandomAffine(0, shear=10, scale=(0.8,1.2)),
                                        transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
                                        transforms.ToTensor(),
                                        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
                                 ])
  
  
  transform = transforms.Compose([transforms.Resize((32,32)),
                                 transforms.ToTensor(),
                                 transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
                                 ])
  training_dataset = CIFAR10(root='D:\PyTorch\cifar-10-python', train=True, download=True, transform=transform_train)
  train_ds, val_ds = random_split(training_dataset, [40000, 10000])
  test_ds = CIFAR10(root='D:\PyTorch\cifar-10-python', train=False, download=True, transform=transform)
  
  train_loader = DataLoader(train_ds, batch_size=100, shuffle=True)
  val_loader = DataLoader(val_ds, batch_size = 100, shuffle = False)
  test_loader = DataLoader(test_ds, batch_size = 100, shuffle=False)
  </code></pre>
  
  <p>This is the model.</p>
  
  <pre><code>class CifarModel(nn.Module):
      def __init__(self):
          super().__init__()
          self.linear = nn.Linear(input_size,  num_classes)
      def forward(self, xb):
          xb = xb.view(-1, 32*32)
          #xb = xb.reshape(-1, 784)
          print(xb.shape)
          out = self.linear(xb)
          return out
  
      def training_step(self, batch):
          images, labels = batch 
          out = self(images)                  # Generate predictions
          loss = F.cross_entropy(out, labels) # Calculate loss
          return loss
  
      def validation_step(self, batch):
          images, labels = batch 
          out = self(images)                    # Generate predictions
          loss = F.cross_entropy(out, labels)   # Calculate loss
          acc = accuracy(out, labels)           # Calculate accuracy
          return {'val_loss': loss.detach(), 'val_acc': acc.detach()}
  
      def validation_epoch_end(self, outputs):
          batch_losses = [x['val_loss'] for x in outputs]
          epoch_loss = torch.stack(batch_losses).mean()   # Combine losses
          batch_accs = [x['val_acc'] for x in outputs]
          epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies
          return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}
  
      def epoch_end(self, epoch, result):
          print("Epoch [{}], val_loss: {:.4f}, val_acc: {:.4f}".format(epoch, result['val_loss'], result['val_acc']))
  
  model = CifarModel()
  </code></pre>
  
  <pre><code>def accuracy(outputs, labels):
      _, preds = torch.max(outputs, dim=1)
      return torch.tensor(torch.sum(preds == labels).item() / len(preds))
  def evaluate(model, val_loader):
      outputs = [model.validation_step(batch) for batch in val_loader]
      return model.validation_epoch_end(outputs)
  
  def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD):
      history = []
      optimizer = opt_func(model.parameters(), lr)
      for epoch in range(epochs):
          # Training Phase 
          for batch in train_loader:
              loss = model.training_step(batch)
              loss.backward()
              optimizer.step()
              optimizer.zero_grad()
          # Validation phase
          result = evaluate(model, val_loader)
          model.epoch_end(epoch, result)
          history.append(result)
      return history
  evaluate(model, val_loader)
  </code></pre>
  
  <p>Here's the error I encounter when I run evaluate function:</p>
  
  <pre><code>torch.Size([900, 1024])
  
  ---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-23-3621eab8de1a&gt; in &lt;module&gt;
       21         history.append(result)
       22     return history
  ---&gt; 23 evaluate(model, val_loader)
  
  &lt;ipython-input-23-3621eab8de1a&gt; in evaluate(model, val_loader)
        3     return torch.tensor(torch.sum(preds == labels).item() / len(preds))
        4 def evaluate(model, val_loader):
  ----&gt; 5     outputs = [model.validation_step(batch) for batch in val_loader]
        6     return model.validation_epoch_end(outputs)
        7 
  
  &lt;ipython-input-23-3621eab8de1a&gt; in &lt;listcomp&gt;(.0)
        3     return torch.tensor(torch.sum(preds == labels).item() / len(preds))
        4 def evaluate(model, val_loader):
  ----&gt; 5     outputs = [model.validation_step(batch) for batch in val_loader]
        6     return model.validation_epoch_end(outputs)
        7 
  
  &lt;ipython-input-22-c9e17d21eaff&gt; in validation_step(self, batch)
       19         images, labels = batch
       20         out = self(images)                    # Generate predictions
  ---&gt; 21         loss = F.cross_entropy(out, labels)   # Calculate loss
       22         acc = accuracy(out, labels)           # Calculate accuracy
       23         return {'val_loss': loss.detach(), 'val_acc': acc.detach()}
  
  ~\Anaconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
     2019     if size_average is not None or reduce is not None:
     2020         reduction = _Reduction.legacy_get_string(size_average, reduce)
  -&gt; 2021     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
     2022 
     2023 
  
  ~\Anaconda3\lib\site-packages\torch\nn\functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
     1834     if input.size(0) != target.size(0):
     1835         raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).'
  -&gt; 1836                          .format(input.size(0), target.size(0)))
     1837     if dim == 2:
     1838         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
  
  ValueError: Expected input batch_size (900) to match target batch_size (300).
  </code></pre>