C1W4A_Build_a_Conditional_GAN at Cell 7

I got this error at # UNQ_C4 like this: ---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [50], in <cell line: 16>()
31 fake_noise = get_noise(cur_batch_size, z_dim, device=device)
33 # Now you can get the images from the generator
34 # Steps: 1) Combine the noise vectors and the one-hot labels for the generator
35 # 2) Generate the conditioned fake images
36
37 #### START CODE HERE ####
—> 38 noise_and_labels = combine_vectors(fake_noise, image_one_hot_labels)
39 fake = gen(noise_and_labels).to(device)
40 #### END CODE HERE ####
41
42 # Make sure that enough images were generated

Input In [43], in combine_vectors(x, y)
4 ‘’’
5 Function for combining two vectors with shapes (n_samples, ?) and (n_samples, ?).
6 Parameters:
(…)
12 with the shape (n_samples, n_classes), but you shouldn’t assume this in your code.
13 ‘’’
14 # Note: Make sure this function outputs a float no matter what inputs it receives
15 #### START CODE HERE ####
—> 16 combined = torch.cat((x.float(),y.float()) ,dim = 1)
17 #### END CODE HERE ####
18 return combined

RuntimeError: Tensors must have same number of dimensions: got 2 and 4
Also at grading ı got error.

Hi @elif_tansu_sunar!

The last line of the stack trace gives you the error:

RuntimeError: Tensors must have same number of dimensions: got 2 and 4

so you know that this line in combine_vectors():
—> 16 combined = torch.cat(...)
received a parameter with 2 dimensions and another parameter with 4 dimensions, but it needs the dimensions to be the same.

Looking up the stack chain, you see combine_vectors was called here:

—> 38 noise_and_labels = combine_vectors(...)

so you need to go back and look at this line to see what parameters it was passing in and decide what’s wrong with the parameters. You know from the error that one has 2 dimensions and one has 4 dimensions, but they should match.

Look at the instructions in the comments right above that line to see if you notice what’s wrong. You might want to put in a print statement temporarily to print the shape of each to help you see the dimensions of each parameter you’re currently passing, and then think about what it should be.

Conceptually, remember that the input for the generator is a noise vector + one-hot vector, but the input for the discriminator is a whole image, including a channel dimension + one-hot-vector matrix.