试读部分
将Tensor或模型移动到指定的设备上:tensor.to(‘cuda:0’) 最开始读取数据时的tensor变量copy一份到device所指定的GPU上去,之后的运算都在GPU上进行 在做高维特征运算的时候,采用GPU无疑是比用CPU效率更高,如果两个数据中一个加了.cuda()或者.to(device),而另外一个没有加,就会造成类型不匹配而报错。 1. Tensor.to(device) 功能:将Tensor移动到指定的设备上。 以下代码将Tensor移动到GPU上: device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”) 1.1 修改dtype a = tensor.to(torch.float64). tensor.dtype : torch.float32 a.dtype : torch.float64 1.2 改变device:用字符串形式给出 a = tensor.to('cuda:0'). tensor.device : device(type=‘cpu’) a.device : device(type=‘cuda’, index=0) 1.3 改变device:用torch.device给出 cuda0 = torch.device('cuda:0') . b = tensor.to(cuda0) . tensor.device : device(type=‘cpu’) b.device : device(type=‘cuda’, index=0) 1.4 同时改变device和dtype c = tensor.to('cuda:0',torch.float64) . other = torch.randn((), dtype=torch.float64, devi……
本文为 VIP 文章,已展示部分内容,
开通 VIP 后查看全文
觉得这篇文章有帮助?
点个赞,或赞赏支持作者持续创作