TypeError when adding cuda device
I'm running a simple demo of Pytorch 1.0, and get stuck when trying cuda settings.(vscode 1.33.1, Python 3.6)

My pytorch code is as followed.

import torch
from torch import cuda
if cuda.is_available():
    devic=cuda.device(0)
    layer=torch.rand([5,3,2],requires_grad=True)


Everything worked fine...But when I tried to add cuda device

layer=torch.rand([5,3,2],requires_grad=True,device=devic)


There raised a TypeError

Traceback (most recent call last):
  File "c:\Users\H\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\ptvsd_launcher.py", line 43, in &lt;module&gt;
    main(ptvsdArgs)
  File "c:\Users\H\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\lib\python\ptvsd\__main__.py", line 410, in main
    run()
  File "c:\Users\H\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\lib\python\ptvsd\__main__.py", line 291, in run_file
    runpy.run_path(target, run_name='__main__')
  File "D:\ProgramData\Anaconda3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "D:\ProgramData\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "D:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\H\Desktop\pth_test\tutorial1.py", line 25, in &lt;module&gt;
    layer1=torch.rand([5,3,2],requires_grad=True,device=devic)
TypeError: rand() received an invalid combination of arguments - got (list, requires_grad=bool, device=device), but expected one of:
 * (tuple of ints size, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool requires_grad)
 * (tuple of ints size, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool requires_grad)


Changing rand() to randn() affected nothing, While empty() and zeros() raised another TypeError

Traceback (most recent call last):
  File "c:\Users\H\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\ptvsd_launcher.py", line 43, in &lt;module&gt;
    main(ptvsdArgs)
  File "c:\Users\H\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\lib\python\ptvsd\__main__.py", line 410, in main
    run()
  File "c:\Users\H\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\lib\python\ptvsd\__main__.py", line 291, in run_file
    runpy.run_path(target, run_name='__main__')
  File "D:\ProgramData\Anaconda3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "D:\ProgramData\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "D:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\H\Desktop\pth_test\tutorial1.py", line 25, in &lt;module&gt;
    layer1=torch.empty([5,3,2],requires_grad=True,device=devic)
TypeError: empty(): argument 'device' must be torch.device, not device


Things are out of control :( Any help will be appreciated

Just exchange devic=cuda.device(0) to devic=torch.device('cuda:0'). 

The - confusing - reason that torch.device is what's used to allocate a tensor to a physical device, while torch.cuda.device is a context manager to tell torch on which gpu to compute stuff.

so if you do

torch.zeros(1, device=torch.device('cuda:0'))


all will be good; if, however, you do

torch.zeros(1, device=torch.cuda.device(0))


you'll get the same error as you did

TypeError: zeros(): argument 'device' must be torch.device, not device

