首页 > 解决方案 > 我应该在哪里更改代码以告诉 PyTorch 不要使用 GPU?

问题描述

如何告诉 PyTorch 不使用 GPU?,为了告诉 PyTorch 不要使用 GPU,您应该在 PyTorch 代码中更改几行。我应该在哪里进行更改?需要修改的代码行在哪里?我试图找到它,但找不到...

标签: pythonpytorch

解决方案


在任何张量或 pytorch 模块上使用该方法.cpu()将组件传输到 ,cpu以便使用它进行计算。

另一个方向是使用方法.to("cpu")。或者,您可以将“cpu”更改为其他设备的名称,例如“cuda”。

例子:

一个)

model = MyModel().cpu()  # move the model to the cpu
x = data.cpu()  # move the input to the cpu
y = model(x)

b)

model = MyModel().to('cpu')  # move the model to the cpu
x = data.to('cpu')  # move the input to the cpu
y = model(x)

推荐阅读