File "C:\Anaconda\lib\site-packages\torch\nn\functional.py", line 2846, in cross_entropy return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing) ValueError: Expected input batch_

username: Ioana_Piscan
cooked: <p>Hi, So I have the following code:</p>
<p>if <strong>name</strong> == ‘<strong>main</strong>’:<br>
import torch<br>
import torchvision<br>
import torchvision.transforms as transforms<br>
import matplotlib.pyplot as plt<br>
import numpy as np<br>
import torch.nn as nn<br>
import torch.nn as nn<br>
import torchvision.transforms as ToTensor<br>
from torch.utils.data import DataLoader<br>
from torch import optim<br>
from torch.utils.data import Dataset<br>
from torchvision import datasets<br>
from torch.autograd import Variable<br>
import torch.nn.functional as F<br>
import torch.optim as optim<br>
import pandas as pd<br>
import os<br>
import csv<br>
from csv import DictReader<br>
from sklearn.utils import shuffle<br>
import matplotlib.pyplot as plt<br>
from torch.utils.data import Dataset, DataLoader<br>
from sklearn.model_selection import train_test_split<br>
import torch.utils.data as data</p>
<p><span class="hashtag">#path</span> = r"E:\Andreea\an 2\tidaim\tidaim\tidaim\Benign\data\train"<br>
<span class="hashtag">#filedirectory</span> = []<br>
<span class="hashtag">#for</span> files in os.listdir(path):</p>
<h1>
<a name="filedirectorys-filedirectoryappendospathjoinpathfiles-1" class="anchor" href="#filedirectorys-filedirectoryappendospathjoinpathfiles-1"></a>filedirectorys = filedirectory.append(os.path.join(path,files))</h1>
<p><span class="hashtag">#filedirectory</span></p>
<p><span class="hashtag">#dataset</span> = ImageFolder(’‘E:\Andreea\tidaim\Benign\data\train’)</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
print(device)</p>
<p>transforms_train = transforms.Compose([transforms.Resize((68, 68)),<br>
transforms.RandomRotation(10.),<br>
transforms.ToTensor()])</p>
<p>transforms_test = transforms.Compose([transforms.Resize((68, 68)),<br>
transforms.ToTensor()])</p>
<p>transforms_validation = transforms.Compose([transforms.Resize((68, 68)),<br>
transforms.ToTensor()])</p>
<p>BATCH_SIZE = 10<br>
LEARNING_RATE = 0.1<br>
TRAIN_DATA_PATH = “./train”<br>
TEST_DATA_PATH = “./test”<br>
VALIDATION_DATA_PATH = “./validation”<br>
TRANSFORM_IMG = transforms.Compose([<br>
transforms.Resize(30),<br>
transforms.CenterCrop(256),<br>
transforms.ToTensor(),<br>
transforms.Normalize(mean=[0.485, 0.456, 0.406],<br>
std=[0.229, 0.224, 0.225] )<br>
])<br>
<span class="hashtag">#dataset</span> = ImageFolder(’‘E:\Andreea\tidaim\Benign\data\train’)<br>
<span class="hashtag">#transform_train</span> = transforms.Compose([</p>
<h1>
<a name="transformstotensor-2" class="anchor" href="#transformstotensor-2"></a>transforms.ToTensor(),</h1>
<h1>
<a name="h-3" class="anchor" href="#h-3"></a>])</h1>
<p><span class="hashtag">#transform_test</span> = transforms.Compose([</p>
<h1>
<a name="transformstotensor-4" class="anchor" href="#transformstotensor-4"></a>transforms.ToTensor(),</h1>
<h1>
<a name="h-5" class="anchor" href="#h-5"></a>])</h1>
<p>train_data = torchvision.datasets.ImageFolder(root=TRAIN_DATA_PATH, transform=transforms_train)<br>
train_data_loader = data.DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True,  num_workers=4)<br>
test_data = torchvision.datasets.ImageFolder(root=TEST_DATA_PATH, transform=transforms_test)<br>
test_data_loader  = data.DataLoader(test_data, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)<br>
validation_data = torchvision.datasets.ImageFolder(root=VALIDATION_DATA_PATH, transform=transforms_validation)<br>
validation_loader = data.DataLoader(validation_data, batch_size=BATCH_SIZE, shuffle=True,  num_workers=4)</p>
<p>net = nn.Sequential(nn.Linear(4624, 64),<br>
nn.ReLU(),<br>
nn.Linear(64, 128),<br>
nn.ReLU(),<br>
nn.Linear(128,3),<br>
)</p>
<p>class MLP(nn.Module):<br>
def <strong>init</strong> (self,n_hidden):<br>
super(MLP, self).<strong>init</strong>()</p>
<pre><code>     self.linear1 = torch.nn.Linear(4624, n_hidden)
     self.linear2 = torch.nn.Linear(n_hidden,3)

     self.activation = torch.nn.ReLU()


 def forward(self,x):
    
      a1 = self.activation(self.linear1(x))
      z2 = self.linear2(a1)


      return z2
</code></pre>
<p>net = MLP(64)</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(net.parameters(), lr=1e-3)<br>
num_epochs = 50</p>
<p>for epoch in range(num_epochs):<br>
net.train()<br>
train_loss=0<br>
total= 0<br>
correct = 0</p>
<pre><code>  for batch_idx, (inputs, targets) in enumerate(train_data_loader):
         correct =  0 
         
         inputs = inputs.view(-1, 4624)
         optimizer.zero_grad()
         outputs = net(inputs)

         loss = criterion(outputs, targets)
         loss.backward()
         optimizer.step()

         train_loss += loss.item()
         _, predicted = torch.max(outputs.data, 1)
         total += targets.size(0)
         #correct = correct + predicted.eq(targets.data).cpu().sum 
</code></pre>
<p>print(‘Results after epoch %d’ % (epoch + 1))</p>
<p>print(‘Training loss: %.3f | Training Acc: %3.f%% )%d%d)’<br>
% (train_loss / (batch_idx + 1), 100. *float(correct) / total, correct, total))</p>
<p>net.eval()</p>
<p>valid_loss = 0<br>
correct = 0<br>
total = 0</p>
<p>for batch_idx, (inputs, targets) in enumerate(validation_loader):<br>
inputs, targets = Variable(inputs), Variable(targets)<br>
inputs = inputs.view(-1, 4624)</p>
<pre><code>  outputs = net(inputs)

  loss = criterion(outputs, targets)

  valid_loss += loss.item()
  _, predicted = torch.max(outputs.data, 1)
  total += targets.size(0)
  correct += predicted.eq(targets.data).cpu().sum()
</code></pre>
<p>print(‘Validation loss: %.3f | Validation Acc: %.3f%% (%d/%d)’<br>
%(valid_loss / (batch_idx + 1), * float(correct)/ total, correct, total))</p>
<p>And I get the following error:</p>
<p>File “C:\Users\Ioana PMEC\Documents\TIDAIM\lab6\cnn2 (1).py”, line 137, in <br>
loss = criterion(outputs, targets)</p>
<p>File “C:\Anaconda\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)</p>
<p>File “C:\Anaconda\lib\site-packages\torch\nn\modules\loss.py”, line 1150, in forward<br>
return F.cross_entropy(input, target, weight=self.weight,</p>
<p>File “C:\Anaconda\lib\site-packages\torch\nn\functional.py”, line 2846, in cross_entropy<br>
return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)</p>
<p>ValueError: Expected input batch_size (30) to match target batch_size (10).</p>
<p>Could someone please help me with this? Thanks</p>

username: ptrblck
cooked: <p>Are you hitting the same shape mismatch error as described in <a href="https://discuss.pytorch.org/t/valueerror-expected-input-batch-size-324-to-match-target-batch-size-4/24498/100">this post</a> or is this a different one?</p>