model.to(device)和model = model.to(device)之间有什么区别?

问题描述 投票:0回答:3

假设模型最初存储在CPU上,然后我想将其移至GPU0,然后可以执行:

device = torch.device('cuda:0')
model = model.to(device)
# or
model.to(device)

这两行之间有什么区别?

python pytorch
3个回答
1
投票

没有语义差异。 nn.Module.to功能将模型移至设备。

但要谨慎。对于张量:

# tensor a is in CPU
device = torch.device('cuda:0')
b = a.to(device)
# a is still in CPU!
# b is in GPU!
# a and b are different 

对于型号:

# model a is in CPU
device = torch.device('cuda:0')
b = a.to(device)
# a and b are in GPU
# a and b point to the same model 

documentation在这里。


1
投票

引用to上的文档:

[在经过训练并保存在GPU上的GPU上加载模型时,只需使用以下命令将初始化模型转换为CUDA优化模型model.to(torch.device('cuda'))。另外,请务必使用.to(torch.device('cuda'))功能在所有模型输入上准备模型的数据。请注意,呼叫my_tensor.to(device)在GPU上返回my_tensor的新副本。它不会覆盖my_tensor。因此,请记住手动覆盖张量:my_tensor = my_tensor.to(torch.device('cuda'))

[通常,当在to上使用torch.nn.Module时,是否保存返回值并不重要,并且作为微优化,实际上最好不保存返回值。在火炬张量上使用时,必须保存返回值-看到您实际上正在收到张量的副本。

参考:Pytorch to()


0
投票

[model = model.to(device)调用一个函数并返回结果,而model.to(device)调用一个函数并丢弃返回的结果。

© www.soinside.com 2019 - 2024. All rights reserved.