How to use batch size with CrossEntropyLoss()

knutj: <p>I am using this code to load image and add date stamp that I want to remove.</p>
<p>import torchvision.transforms as transforms</p>
<p>transform = transforms.Compose(<br>
[transforms.ToTensor()])</p>
<p>class ImageL(Dataset):<br>
def <strong>init</strong>(self,folder,width,height,transform):<br>
self.folder = folder<br>
self.transform = transform<br>
self.images =  glob.glob(os.path.join(folder,‘jpg’,’<em>’,’</em>.jpg’))<br>
self.toal_imgs = natsorted(self.images)</p>
<pre><code>    self.ts = time.time()
    self.ts = datetime.datetime.fromtimestamp(self.ts).strftime('%d-%m-%Y_%H-%M-%S')
    self.width = width
    self.heigth_im = height
def __len__(self):
    return len(self.toal_imgs)

def __getitem__(self, i):
    st = self.ts
    width = self.width
    height = self.heigth_im
    font = cv2.FONT_HERSHEY_SIMPLEX

    img_loc = self.toal_imgs[i]
    img = cv2.imread(img_loc)

    img_noise = cv2.putText(img, st, (10, 500), font, 1, (255, 255, 255), 2)
    img_noise = cv2.resize(img_noise, (width, height))
    #img_noise = img_noise.astype('float32') / 255

    img = cv2.resize(img, (width, height))

    #img = img.astype('float32') / 255

    imp = np.asarray(img)
    img_noise = np.asarray(img_noise)

    #img = np.moveaxis(img,2,0)
    #img_noise = np.moveaxis(img_noise,2,0)

    img = self.transform(img)
    img_noise = self.transform(img_noise)
    return (img_noise,img)
</code></pre>
<p>batch_size = 32<br>
width = 48<br>
height = 48<br>
my_dataset = ImageL(’/Users/knutjorgenbjuland/PycharmProjects/autoencoder’,width,height,transform)<br>
trainset = data.DataLoader(my_dataset , batch_size=batch_size, shuffle=False,<br>
num_workers=4, drop_last=True)</p>
<p>I used this as source for my code, <a href="https://medium.com/@garimanishad/reconstruct-corrupted-data-using-denoising-autoencoder-python-code-aeaff4b0958e" rel="noopener nofollow ugc">https://medium.com/@garimanishad/reconstruct-corrupted-data-using-denoising-autoencoder-python-code-aeaff4b0958e</a></p>
<p>However when I am using loss = criterion(outputs.,labels)</p>
<p>I get this error,</p>
<p>RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4</p>

ptrblck: <p><code>nn.CrossEntropyLoss</code> expects model outputs with a class dimension as <code>[batch_size, nb_classes, *additional_dims]</code>, while the target should not contain this class dimension but instead <code>[batch_size, *additional_dims]</code> and its values should contain the class indices in the range <code>[0, nb_classes-1]</code> as described in the <a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=crossentropyloss#torch.nn.CrossEntropyLoss">docs</a>.</p>
<p>You are not supposed to set a batch size for any layer or criterion in PyTorch and I guess your target might be one-hot encoded and has thus the additional unwanted dimension.<br>
If that’s the case, use <code>target = torch.argmax(target, dim=1)</code> to create the target tensor in its expected shape.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>