C1W1 assignment's code is working fine but I am getting review

Hi,

My code works perfectly, however, I have received a review.

This is the grader’s output but I dont see anything wrong with the code.

Cell #UNQ_C3. Can’t compile the student’s code. Error: SyntaxError(‘invalid syntax’, (’/tmp/student_solution_cells/cell_6.py’, 22, 15, ’ noiseVector=torch.ones(n_samples, z_dim,device=device)\n’))

I am resubmitting with minor changes; I tried with randn as well as torch.normal. The code executes perfectly for both of them.

But I am not sure why I am getting the syntax error.

From my end the entire code is working fine and generating the output as displayed in the notebook.

Please review the code and let me know the actual cause.

Regards,

Gaurang

Hi @Gaurang_Sharma,

The auto-grader can sometimes be touchy, usually when it runs across something it doesn’t expect. This may be a situation where we want to enhance the auto-grader to be more lenient, but let’s start by seeing if we can narrow down the issue and get the auto-grader to accept your code.

A few questions:

Is the line:
noiseVector=torch.ones(n_samples, z_dim,device=device)
within the #### START CODE HERE #### … #### END CODE HERE #### portion of #UNQ_C3?
Do you need that line when you try your randn implementation?
Can you see if you can implement that section without the noiseVector... line?

Hi @Wendy ,

I have submitted up to nine solutions,
The above is one of them.

The below code error was from my first solution, and I tried the same today as well,
Cell #UNQ_C3. Can’t compile the student’s code. Error: SyntaxError(‘invalid syntax’, (’/tmp/student_solution_cells/cell_6.py’, 22, 10, ’ return torch.randn(n_samples, z_dim, device=device)\n’))

I have tried multiple attempts and they all seem to be working.
I even tried various random generation functions; they all worked fine with output.

I am not sure of its further optimization.

I even tried the noise generation function from the 2nd assignment exercise.
Even that is not clear as the submission (which got graded in the 2nd week).

Could you please help me with it?

Regards,
Gaurang

Hmm. So it’s not your implementation. My guess is that something just above this line was inadvertently edited, confusing the auto-grader. It can be very touchy.

I expect it will work if you refresh the assignment and copy in your code changes to the new copy. Instructions for how to refresh your assignment are here: Coursera | Online Courses & Credentials From Top Educators. Join for Free | Coursera

I’m also happy to take a look if you want to DM me your ipynb file for this assignment.

HI @Wendy,

Thanks a lot, The same code worked after refreshing the workspace.

Regards,
Gaurang

{mentor edit: code removed}
For me, the solution was to change the return statement from:
torch.randn(n_samples, z_dim, device=device)
to:
torch.randn(n_samples, z_dim).to(device)

Please do not post your code on the forum. That’s not allowed by the Code of Conduct.

The description of the change you made is sufficient.

Hi,

I am working on the assignment C1W1_Your_First_GAN. I recieved a compile error when I upload my code, even though it executed successfully in the notebook and I was able to also see the results in the optional part. I first saw this grader error: “Cell #15. Can’t compile the student’s code. Error: NameError(“name ‘batch’ is not defined”,)”.

I made a copy of the notebook as discussed on the forum, copied all the code again just to ensure my notebook is clean.
I uploaded the clean copy but the grader still gave me the same error.

Then I changed the name of my one variable in get_disc_loss and get_gen_loss from “batch” to “gen_batch” to see the result. The line now reads gen_batch = pass the noise to the generator.
It executes successfully in the notebook, but upon submission the grader is now giving me:

Can’t compile the student’s code. Error: IndexError(‘slice() cannot be applied to a 0-dim tensor.’,)

Your help will be appreciated.
Regards
Roelof

I suggest you go back to the original version and debug it. When you have the situation that your code runs correctly in the notebook, but fails the grader, particularly with a variable name not defined type of error, it means you are probably referencing global variables someplace within the local scope of your functions. You have to be very careful here that they are using fairly sophisticated python syntax in that they are passing function references as arguments to your functions. You have to be careful to reference (call) the given functions as opposed to calling the function by the global name that happened to be passed as the argument in that case. What happens if the grader passes a different function as that argument? That’s just one suggestion of what I mean by referencing global variables. In how many places does the variable name batch appear? Is there any way you can see that those references might be interfered with by the type of bug I’m describing here?

If that type of hint is not enough to get you there, then we’ll probably need to look at your code, but we’ll need to do that in a private rather than public way to play within the rules here. I’ll send you a DM about how to proceed in that case.

1 Like

Thanks Paul. I modified the variable that was giving trouble from batch to gen_batch. I am able to run the optional code successfully as well. However, when I upload my code, it now gives me the error: Cell #15. Can’t compile the student’s code. Error: IndexError(‘slice() cannot be applied to a 0-dim tensor.’,)

Yes, you said that earlier. So just looking at this as a scientist, the evidence is that you have two versions of the code which are wrong in two distinct ways. So as I alluded above, you have two choices: debug the first version or debug the second version. My previous suggestion was that debugging the first version would be my choice because it seemed clearer where to look for the problem. If the suggestions I gave above about the type of problem to look for are not sufficient to “get you there”, then please see your DMs for further suggestions. You should have a DM (“direct message”) from me. You can recognize a DM versus a reply on a public thread by the little “envelope” icon.

Thanks for solving my problem Paul. For others, I was calculating the get_disc_loss by using torch.Tensor() to calculate the average loss. It is not necessary since the real loss and fake loss are already tensors. You just need to add the fake and real loss and divide by two. I assume using the mean function would also work. Wrestling with the code actually helped me understand it better. I hope it does the same for you!

1 Like