Leo*_*ckl 6 python gpu pytorch
在 PyTorch 中,以下两种方法向 GPU 发送张量(或模型)有什么区别:
设置:
X = np.array([[1, 3, 2, 3], [2, 3, 5, 6], [1, 2, 3, 4]]) # X = model()
X = torch.DoubleTensor(X)
Run Code Online (Sandbox Code Playgroud)
| 方法一 | 方法二 |
|---|---|
X.cuda() |
device = torch.device("cuda:0")X = X.to(device) |
(我真的不需要对后端发生的事情进行详细解释,只想知道它们是否本质上都在做同样的事情)
Sha*_*hai 10
两者没有区别。
pytorch的早期版本曾.cuda()和.cpu()方法从CPU移到张量和模型,以GPU和背部。然而,这使得代码编写有点麻烦:
if cuda_available:
x = x.cuda()
model.cuda()
else:
x = x.cpu()
model.cpu()
Run Code Online (Sandbox Code Playgroud)
后来引入的版本.to()基本上以优雅的方式处理所有事情:
device = torch.device('cuda') if cuda_available else torch.device('cpu')
x = x.to(device)
model = model.to(device)
Run Code Online (Sandbox Code Playgroud)
它们的语法略有不同,但它们是等效的:
\n| \xe2\xa0\x80 | 。命名) | .to(设备) | .cuda() |
|---|---|---|---|
| 中央处理器 | to(\'cpu\') | to(torch.device(\'cpu\')) | cpu() |
| 当前GPU | to(\'cuda\') | to(torch.device(\'cuda\')) | cuda() |
| 特定GPU | to(\'cuda:1\') | to(torch.device(\'cuda:1\')) | cuda(device=1) |
注意:当前的cuda设备是0默认的,但是可以使用 进行设置torch.cuda.set_device()。