PyTorch:tensor.cuda()和tensor.to(torch.device("cuda:0&";))之间有什么区别? [英] PyTorch: What is the difference between tensor.cuda() and tensor.to(torch.device("cuda:0"))?

查看:530
本文介绍了PyTorch:tensor.cuda()和tensor.to(torch.device("cuda:0&";))之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PyTorch中,以下两种方法之间的区别在于将张量(或模型)发送到GPU:

In PyTorch, what is the difference between the following two methods in sending a tensor (or model) to GPU:

设置:

X = np.array([[1, 3, 2, 3], [2, 3, 5, 6], [1, 2, 3, 4]]) # X = model()
X = torch.DoubleTensor(X)

<身体>
方法1 方法2
X.cuda() device = torch.device("cuda:0")
X = X.to(device)


(我真的不需要详细说明后端发生的事情,只想知道它们是否本质上都在做同一件事)


(I don't really need a detailed explanation of what is happening in the backend, just want to know if they are both essentially doing the same thing)

推荐答案

两者没有区别.
pytorch的早期版本具有 .cuda() .cpu()方法,可将张量和模型从cpu移至gpu并移回gpu.但是,这使代码编写有些麻烦:

There is no difference between the two.
Early versions of pytorch had .cuda() and .cpu() methods to move tensors and models from cpu to gpu and back. However, this made code writing a bit cumbersome:

if cuda_available:
  x = x.cuda()
  model.cuda()
else:
  x = x.cpu()
  model.cpu()

后来的版本引入了 .to(),它基本上以一种优雅的方式处理了所有事情:

Later versions introduced .to() that basically takes care of everything in an elegant way:

device = torch.device('cuda') if cuda_available else torch.device('cpu')
x = x.to(device)
model = model.to(device)

这篇关于PyTorch:tensor.cuda()和tensor.to(torch.device("cuda:0&";))之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆