C3W1 combine_sample input device assertion error

I am working on UNQ_C1 and my code passes all of the checks except for the very last one: “Check that the solution matches the input device”. The code I wrote doesn’t specify a device. I see that earlier in the notebook the device is set to “cuda”. What would cause this error and how should I fix it?

my lab ID is iyflziuugnhl. Thanks!

AssertionError Traceback (most recent call last)
Input In [34], in <cell line: 38>()
37 assert torch.abs(test_combination - test_reals).sum() < 1e-4
38 if torch.cuda.is_available():
39 # Check that the solution matches the input device
—> 40 assert str(combine_sample(
41 torch.ones(n_test_samples, 10, 10).cuda(),
42 torch.zeros(n_test_samples, 10, 10).cuda(),
43 0.8
44 ).device).startswith(“cuda”)
45 print(“Success!”)

Are you sure that your combine_sample logic does not manipulate the device anywhere? Note what that assertion does: it constructs a test case in which both input data are on device “cuda” and then checks that the return value of the function is also on device “cuda”. If that assertion fails, then your logic must have done something that changes the device assignment.

E.g. did you use the clone() method to create new copies of real and fake? That should preserve the device assignments. But perhaps you used some other way of creating the copies that does not preserve that.

Also note that the mentors cannot directly look at anyone else’s notebooks, so there is no purpose in including the lab ID.

Thanks for your reply. I never used the tensor.clone() hint. I guess I didn’t need it with the way I constructed my function. (Received full score when I submitted the code.)

I ended up fixing the issue by explicitly including the parameter device=real.device in the call to torch.empty() when I created target_images.

My interpretation is that if you use real.clone() then you never need torch.empty in the first place, so you totally avoid the problem with the device. It simplifies the code: you don’t even have to mention the device.