简体   繁体   English

有没有办法确定 PyTorch model 是在 CPU 上还是在设备上?

[英]Is there a way to figure out whether PyTorch model is on cpu or on the device?

I would like to figure out, whether the PyTorch model is on cpu or cuda in order to initialize some other variable as Torch.Tensor or Torch.cuda.Tensor depending on the model. I would like to figure out, whether the PyTorch model is on cpu or cuda in order to initialize some other variable as Torch.Tensor or Torch.cuda.Tensor depending on the model.

However, looking at the output of the dir() function I see only .cpu() , .cuda() , to() methods which put the model on device, GPU or other device, specified in to. However, looking at the output of the dir() function I see only .cpu() , .cuda() , to() methods which put the model on device, GPU or other device, specified in to. For PyTorch tensor there is is_cuda attribute, but no analogue for the whole model.对于PyTorch张量有is_cuda属性,但对于整个 model 没有类似物。

Is there some way to deduce this for a model, or one needs to refer to a particular weight?有什么方法可以为 model 推断这一点,还是需要参考特定的重量?

No, there is no such function for nn.Module , I believe this is because parameters could be on multiple devices at the same time.不, nn.Module 没有这样的nn.Module ,我相信这是因为参数可以同时在多个设备上。

If you're working with a single device, a workaround is to check the first parameter:如果您使用的是单个设备,解决方法是检查第一个参数:

next(model.parameters()).is_cuda

As described here .如此所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pytorch模型问题:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0 - Pytorch model problem: RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0 Pytorch CPU CUDA 设备负载无 gpu - Pytorch CPU CUDA device load without gpu Pytorch CPU 设备索引不能为负数 - Pytorch CPU Device index must not be negative PyTorch:是否可以将模型存储在CPU ram中,但是对于大型模型,可以在GPU上运行所有操作? - PyTorch: Is there a way to store model in CPU ram, but run all operations on the GPU for large models? 无法弄清楚 pytorch 代码中的就地操作? - Unable to figure out inplace operation in the pytorch code? 无法找出 pytorch 中的就地操作错误 - Unable to figure out the In place operation error in pytorch 从 Pytorch 中的 GPU/CPU 中删除 model - Delete model from GPU/CPU in Pytorch Pytorch Model 预测 GPU 或 CPU 速度提升 - Pytorch Model Prediction on GPU or CPU speed improvement Pytorch model 训练 CPU Memory 泄漏问题 - Pytorch model training CPU Memory leak issue 判断一个模型是pytorch模型还是tensorflow模型还是scikit模型 - Determine whether a model is pytorch model or a tensorflow model or scikit model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM