Course 1 Week 3 WGAN error in Putting It All Together

I successfully completed the WGAN lab but the last part (Putting It All Together), where no coding is required, gave me a run error when I tried to see the final results. Did not affect my submission or grade.


ValueError Traceback (most recent call last)
in
25
26 # Keep track of the average critic loss in this batch
—> 27 mean_iteration_critic_loss += crit_loss.item() / crit_repeats
28 # Update gradients
29 crit_loss.backward(retain_graph=True)

ValueError: only one element tensors can be converted to Python scalars

1 Like

Hey @pdobosh, welcome to the DeepLearning.AI community!

I get nothing of the sort when running the notebook :thinking:

From that error message, it seems that your problem is with crit_loss.item().
torch.Tensor.item only works for tensors with one element (c.f. documentation), so I believe your crit_loss has more than just one element.

This crit_loss comes from the get_crit_loss function just above, so that must be returning a tensor with multiple elements.
Now, this might be because of a problem in the function itself, or its inputs — which themselves come from other functions you implemented.

One of your functions is not behaving as it should, and you’ll have to investigate it.
This should have been covered by the unit tests, but it seems that you may have found a loophole :grin:

Here are the expected shapes for some of the variables in that last code cell:

epsilon.shape = torch.Size([128, 1, 1, 1])
gradient.shape = torch.Size([128, 1, 28, 28])
gp.shape = torch.Size([])
crit_loss.shape = torch.Size([])

gp and crit_loss are both be scalars.

Try to see which of these is not a match for you, and investigate the function it’s coming from.
If you’re still stuck, feel free to come back with more questions or a screenshot of your tensor shapes and the function you think is not correct.
If you do find the fix, let us know what it was so we can adapt the unit test accordingly as well.

Hope this helps you, cheers!

2 Likes

Also, try restarting your notebook and running all cells again — just as a sanity check.
I seem to be getting some weird results when running things out of order.

Figured it out thank you!!! You can see some notes below. The problem was that I was getting a 128 element tensor instead of a scalar. I went back and looked carefully at the instructions and saw that I was supposed to use the means of the crit_fake_pred and crit_real_pred, just as we had done for get_gen_loss. I fixed that and it seem to be running all the way.

Where the unit tests fail is that the get_crit_loss test only uses single element tensors in the test.

Thanks for your great help!

I restarted the notebook and got these shapes:
torch.Size([128, 1, 1, 1])
torch.Size([128, 1, 28, 28])
torch.Size()
torch.Size([128, 1]) <------- whoa!
I printed out crit_loss inside my get_crit_loss function and got these values
for the unit test:

tensor(-0.7000)
tensor(60.)

but for the final code:
tensor([[9.2841],
[9.2800],
[9.2909],
[9.2720],
[9.2881],
[9.2977],
[9.2866],
[9.2713],
[9.2932],
[9.2890],
[9.2956],
[9.2716],
[9.2689],
etc.

I will continue to pursue this.

1 Like

Thanks for the nice discussion! I didn’t use the mean either, and this thread helped me. Thank you for sharing the expected shapes as well

Same thing here. Perhaps it’s a good idea to rewrite the last UNIT TEST so that it has an example with tensors that are not scalars.

Just before UNQ_C4, I read the following instruction sentence slowly 4 times before getting it:

The arguments are the scores for all the images in the batch, and you will use the mean of them.

Like math and code, instructions require calm, repeated reading (esp. when the code doesn’t run correctly:)