NameError: name 'torch' is not defined

Hi all, I’m able to import torch but am getting an error about torch not being defined (NameError: name ‘torch’ is not defined) for the following statement:

device = torch.device(‘cuda’) if torch.cuda.is_available() else torch.device(‘cpu’)

Background: do not have cuda, just using cpu. python version: 3.7.6, pytorch ver: 1.5.1, using conda. Any insights would be greatly appreciated, thanks

Could you post the exact stack trace and error message, please?

Sometimes when I encounter this, restarting the Python kernel and re-importing torch does the trick.

1 Like

you can use the option execution enviroment → change type of execution enviroment → aceleration by hardaware : none