from torch import nn
import torch
# Set device to cuda if cuda is available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Create model
model = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
# moving model to GPU
model.to(device)
DataParallel
也应该在单个GPU上工作,但是您应该检查args.gpus
是否只包含要使用的设备的id(应该是0)还是None
。 选择None
将使模块使用所有可用的设备。在您也可以删除
DataParallel
,因为您不需要它,只需调用model.cuda()
或者,我更喜欢的是,model.to(device)
,其中device
是设备的名称。在示例:
这个例子展示了如何在单个GPU上使用一个模型,使用
.to()
而不是.cuda()
来设置设备。在如果您想使用
^{pr2}$DataParallel
,可以这样做相关问题 更多 >
编程相关推荐