All cells passed the unit test, but got error after submitting

I got below error saying that the cell 9 cannot be compiled, because torch has no attribute of square. Any idea why it happens?

1 Like

Please check what happens if you click “Kernel → Restart and Clear Output”, “Save” and then “Cell → Run All” in the notebook. Then carefully page through and check all the cells. Are you sure you don’t see any errors similar to the one from the grader?

It still does not work after I follow the steps you mentioned. I think the problem happens in this code: This is how I calculate the penalty, is there something wrong with it?

image

Very interesting. Well torch.square should be fine. But in python you can overwrite variables and even reserved words in some cases. Are you sure that you don’t assign anything to the variable torch anywhere in your code?

Here’s a contrived example of the sort of bug that I’m thinking might have happened but using np instead of torch:

a = np.array([1, 2, 4])
print(f"a = {a}")
b = np.square(a)
print(f"b = {b}")
np = 42.
c = np.square(a)
a = [1 2 4]
b = [ 1  4 16]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-f1ab1f756ba9> in <module>
      4 print(f"b = {b}")
      5 np = 42.
----> 6 c = np.square(a)

AttributeError: 'float' object has no attribute 'square'

I searched the entire code with “torch” and I don’t see that I assign any value to variable torch. All places use torch.xxxxx to call certain functions.

Interesting. Ok, I think it’s time to look at the source code. Please check your DMs for a message from me.

Ok, I can reproduce your results. It turns out that torch.square works in the notebook, but not for the grader. I tried implementing that function three different ways:

#### START CODE HERE ####
# penalty = torch.mean(torch.square(gradient_norm - 1))
penalty = torch.mean(torch.pow(gradient_norm - 1,2))
# penalty = torch.mean((gradient_norm - 1)**2)
#### END CODE HERE ####

All three of them work in the notebook, but only the torch.pow and **2 versions will pass the grader.

So it must be the case that the grader is using a different version of torch that somehow does not support the square function. I checked the current Torch documentation and it doesn’t say anything about “square” being deprecated or the like, so this is something of a mystery. I will try to either file a bug or (failing that) at least report this to the Course Staff in case they think it’s something worth figuring out.

2 Likes

Further followup: I went into my Your First GAN assignment (GANs C1 W1) and just added a call to torch.square in one of the graded functions in a way that had no effect on the actual return value of the function. I get the exact same result:

The grader throws the same error shown above about torch.square not being defined.

But if I rewrite the “extra” code using torch.pow(:, 2) then it’s all fine.

1 Like

I would just like to add that I also got the exact same error after passing all asserts, and replacing it with torch.pow(:,2) worked for me. Thanks