I am getting the following runtime error in Lab 1 week 1:
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat1 in method wrapper_addmm)
Here is my code:# UNQ_C6 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
GRADED FUNCTION: get_disc_loss
def get_disc_loss(gen, disc, criterion, real, num_images, z_dim, device):
‘’’
Return the loss of the discriminator given inputs.
Parameters:
gen: the generator model, which returns an image given z-dimensional noise
disc: the discriminator model, which returns a single-dimensional prediction of real/fake
criterion: the loss function, which should be used to compare
the discriminator’s predictions to the ground truth reality of the images
(e.g. fake = 0, real = 1)
real: a batch of real images
num_images: the number of images the generator should produce,
which is also the length of the real images
z_dim: the dimension of the noise vector, a scalar
device: the device type
Returns:
disc_loss: a torch scalar loss value for the current batch
‘’’
# These are the steps you will need to complete:
# 1) Create noise vectors and generate a batch (num_images) of fake images.
# Make sure to pass the device argument to the noise.
# 2) Get the discriminator’s prediction of the fake image
# and calculate the loss. Don’t forget to detach the generator!
# (Remember the loss function you set earlier – criterion. You need a
# ‘ground truth’ tensor in order to calculate the loss.
# For example, a ground truth tensor for a fake image is all zeros.)
# 3) Get the discriminator’s prediction of the real image and calculate the loss.
# 4) Calculate the discriminator’s loss by averaging the real and fake loss
# and set it to disc_loss.
# Important: You should NOT write your own loss function here - use criterion(pred, true)!
#### START CODE HERE ####
#torch.device(device)
random_numbers = get_noise(num_images,z_dim, device)
#print(random_numbers)
fake_images = gen(random_numbers).detach()
#print(fake_images)
predicted_fake = disc(fake_images)
disc_loss_fake = criterion(predicted_fake,torch.zeros_like(predicted_fake))
#print(disc_loss_fake)
predicted_real = disc(real)
disc_loss_real = criterion(predicted_real,torch.ones_like(predicted_real))
#print(disc_loss_real)
disc_loss = (disc_loss_real + disc_loss_fake) * 0.5
#print(disc_loss)
#### END CODE HERE ####
return disc_loss
What am I missing?