不同设备上的张量可以相加吗?
最近我发现了一件奇怪的事情。据我所知,当你想对两个张量进行一些操作时,你应该确保它们位于同一设备上。但是,当我这样编写代码时,它会意外运行
import torch
a = torch.tensor(1, device='cuda')
print(a.device)
b = torch.tensor(2, device='cpu')
print(b.device)
torch(a+b)
cuda:0
cpu
tensor(3, device='cuda:0')
并且无法在我的代码中工作,如下所示:
pts_1_tile = torch.tensor([[0], [0]], dtype=torch.float32)
torch.add(pred_4pt_shift, pts_1_tile)
这里 pred_4pt_shift
是子网的中间结果,它是 GPU 上的张量。 我的问题是,为什么第一个代码可以工作,但第二个代码报告这个不同的设备错误?
I found a curious thing recently. As far as I know, when you want to do some operations on two tensors, you should make sure that they are on the same device. But when I write my code like this, it runs unexpectly
import torch
a = torch.tensor(1, device='cuda')
print(a.device)
b = torch.tensor(2, device='cpu')
print(b.device)
torch(a+b)
cuda:0
cpu
tensor(3, device='cuda:0')
And it can't work in my code like this:
pts_1_tile = torch.tensor([[0], [0]], dtype=torch.float32)
torch.add(pred_4pt_shift, pts_1_tile)
here pred_4pt_shift
is an intermediate result of a sub-Net, and it is a tensor on GPU.
My question is that why the first code can work but the second one reports this different device error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你的意思是
print(a+b)
而不是torch(a+b)
。标量张量是一种特殊情况,可以自动移动到目标设备。
如果将
a
和b
定义为一维张量,则会出错:I guess you mean
print(a+b)
rather thantorch(a+b)
.Scalar tensor is a special case which could be automatically moved to target device.
If you define
a
andb
as 1-d tensor, it will error out: