Pytorch:Fundamentals Module 4 (Building a Robust CNN)

Exercise 4: I’m getting the following error while running validate_epoch():

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

The model instantiation cell is locked (model = SimpleCNN(num_classes)), so I can’t add model.to(device). Could someone help identify what might be causing this device mismatch?

Perhaps you have accidentally deleted Cell 3. In Exercise 4, the line that moves the tensors to the device is already provided as a non-graded line.

If Cell 3 is missing, you will need to reset the assignment. To do this, click on the three dots in the top-right corner. First, rename or delete the current assignment, and then restore the original. This should give you a new copy of the assignment with the original cells restored.

hi @Nivedita26

Can you post screenshot of the complete error, that can give mentors hint on where your code might be incorrect.

Please make sure not to post any part of grade function codes as it is against community guidelines to post on public thread.

In case your error log is lengthy, you can take two separate screenshots and post it.

or check the below code instruction in validate epoch which mentions

 Move images and labels to the specified device

You need to move images and label to the below argument mentioned in the screenshot :backhand_index_pointing_down:

Same code instruction is mentioned! in train_epoch, so your error is probably pointing that your specific device have conflicting variable recall.

Regards
Dr. Deepti

@Nivedita26

Kindly click on my name and then message me screenshot of your codes for train epoch and validate epoch.

as per the log info, it is pointing your labels created the issue with device allocation, so in my previous response here where I mentioned move the image and labels to specified device, chances are, there might conflict of variable recall with the assigned arguments

for example if I have to move image to say tpu (this is example not directed to codes directly)

images.to(tpu), same goes for the labels as labels.to(tpu) (for the grade function the device is recalled by argument device, so I hope you have followed the same

Hii, This is my cell 3.

Moderator has removed code solutions

Hi, I’ve removed the screenshots of your code solutions because sharing solution screenshots publicly is not allowed under the Code of Conduct here.

The mentor is asking if you can send the screenshots to them via private inbox. You can either do that, or you can send them to me privately through my inbox, and I’ll be happy to check the code as well. Please just make sure not to post the solutions publicly.

Hi, sorry about that. I didn’t realize posting the screenshots publicly wasn’t allowed. I’ll send them through a private inbox instead. Thanks for letting me know!

After looking at the notebook, the cause of the error in the exercise was mismatch in usage of the variable name.

First initialised as outputs (plural) was later being used as output (singular) to get predictions.

@Nivedita26

apologies i couldn’t check your codes as I am having fever and cold. As per mubsi’s response looks like your issue is resolved.

Regards
Dr. Deepti