Issue training pytorch model on gpu
I am trying to implement the discriminator of a basic MNIST GAN in PyTorch. When I run training on the CPU, it works without any issue giving the desired output. However, when I run it on the GPU it shows a runtime error. I am pasting the code for my model as well as my training below along with the modifications I made to try and run training on the GPU.

dev = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

def preprocess(x):
    return x.view(-1,1,28,28).to(dev)

discriminator = nn.Sequential(
    Lambda(preprocess),
    nn.Conv2d(1,64,3,stride=2,padding=1),
    nn.LeakyReLU(negative_slope=0.2),
    nn.Dropout(0.4),
    nn.Conv2d(64,64,3,stride=2,padding=1),
    nn.LeakyReLU(negative_slope=0.2),
    nn.Dropout(0.4),
    Lambda(lambda x:x.view(x.size(0),-1)),
    nn.Linear(3136,1),
    nn.Sigmoid()
)
loss = nn.BCELoss()
opt = optim.Adam(discriminator.parameters(),lr = 0.002)

discriminator.to(dev)


def train_discriminator(model, dataset,opt, n_iter=100, n_batch=256):
    half_batch = int(n_batch / 2)
    for i in range(n_iter):
        X_real, y_real = generate_real_samples(dataset, half_batch)
        error = loss(model(X_real),y_real)
        error.backward()
        X_fake, y_fake = generate_fake_samples(half_batch)
        error = loss(model(X_fake),y_fake)
        error.backward()
        opt.step()


Now on running train_discriminator(discriminator,dataset,opt)
I get the following error which I cannot make sense of.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-15-ee20eb2a8e55&gt; in &lt;module&gt;
----&gt; 1 train_discriminator(discriminator,dataset,opt)

&lt;ipython-input-13-9e6f9b4874c8&gt; in train_discriminator(model, dataset, opt, n_iter, n_batch)
      3     for i in range(n_iter):
      4         X_real, y_real = generate_real_samples(dataset, half_batch)
----&gt; 5         error = loss(model(X_real),y_real)
      6         error.backward()
      7         X_fake, y_fake = generate_fake_samples(half_batch)

~/environments/workspace/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--&gt; 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~/environments/workspace/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    496 
    497     def forward(self, input, target):
--&gt; 498         return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
    499 
    500 

~/environments/workspace/lib/python3.7/site-packages/torch/nn/functional.py in binary_cross_entropy(input, target, weight, size_average, reduce, reduction)
   2075 
   2076     return torch._C._nn.binary_cross_entropy(
-&gt; 2077         input, target, weight, reduction_enum)
   2078 
   2079 

RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' in call to _thnn_binary_cross_entropy_forward


Would really appreciate it if anyone could suggest any changes that are needed to be made that will solve this issue.

According to the error message, the ground truths are not in GPU: 


  RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' in call to _thnn_binary_cross_entropy_forward

