Hello, I am new to pytorch and I am just trying to get dcgan to work on mnist and I am really struggling with this error Target and input must have the same number of elements. target nelement (128) != input nelement (307328). I used the dataloader given in mnist and here is my code pasted below, it would be greatly appreciated if somebody could point me in the right direction.
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import torchvision.datasets as dset
import torchvision.transforms as transforms
import numpy as np
from dcgan import weights_init, Generator, Discriminator
data_dir="/home/sean/Documents/Gans"
params = {
“bsize” : 128,# Batch size during training.
‘imsize’ : 64,# Spatial size of training images. All images will be resized to this size during preprocessing.
‘nc’ : 1,# Number of channles in the training images. For coloured images this is 3.
‘nz’ : 100,# Size of the Z latent vector (the input to the generator).
‘ngf’ : 64,# Size of feature maps in the generator. The depth will be multiples of this.
‘ndf’ : 64, # Size of features maps in the discriminator. The depth will be multiples of this.
‘nepochs’ : 10,# Number of training epochs.
‘lr’ : 0.0002,# Learning rate for optimizers
‘beta1’ : 0.5,# Beta1 hyperparam for Adam optimizer
‘save_epoch’ : 2}# Save step.
device = torch.device(“cuda:0” if(torch.cuda.is_available()) else “cpu”)
print(device, " will be used.\n")
transform = transforms.Compose([transforms.Scale(params[‘imsize’]),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])])
mnist_data = dset.MNIST(root=data_dir,
train=True,
transform=transform,
download=True)
dataloader = torch.utils.data.DataLoader(dataset=mnist_data,
batch_size=params[‘bsize’],
shuffle=True,drop_last=True)
print(‘Finish Loading Dataset’)
netG=Generator(params).to(device)
netG.apply(weights_init)
print(netG)
netD=Discriminator(params).to(device)
netD.apply(weights_init)
print(netD)
criterion=nn.BCELoss()
fixed_noise=torch.randn(64,params[‘nz’],1,1,device=device)
real_label = 1
fake_label = 0
Optimizer for the discriminator.
optimizerD = optim.Adam(netD.parameters(), lr=params[‘lr’], betas=(params[‘beta1’], 0.999))
Optimizer for the generator.
optimizerG = optim.Adam(netG.parameters(), lr=params[‘lr’], betas=(params[‘beta1’], 0.999))
Training Loop
Stores generated images as training progresses.
img_list = []
Stores generator losses during training.
G_losses = []
Stores discriminator losses during training.
D_losses = []
iters = 0
print(“Starting Training Loop…”)
print("-"*25)
for epoch in range(params[‘nepochs’]):
for i, data in enumerate(dataloader, 0):
############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
# train with real
netD.zero_grad()
real_cpu = data[0].to(device)
batch_size = real_cpu.size(0)
label = torch.full((batch_size,), real_label, device=device)
    output = netD(real_cpu)
    errD_real = criterion(output, label)
    errD_real.backward()
    D_x = output.mean().item()

    # train with fake
    noise = torch.randn(batch_size, nz, 1, 1, device=device)
    fake = netG(noise)
    label.fill_(fake_label)
    output = netD(fake.detach())
    errD_fake = criterion(output, label)
    errD_fake.backward()
    D_G_z1 = output.mean().item()
    errD = errD_real + errD_fake
    optimizerD.step()

error is coming from the loss function at criterion