Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pytorch中的model.cuda()_Pytorch - Fatal编程技术网

pytorch中的model.cuda()

pytorch中的model.cuda(),pytorch,Pytorch,如果我在pytorch中调用model.cuda(),其中model是nn.Module的子类,并说如果我有四个GPU,它将如何利用这四个GPU,以及如何知道使用的是哪个GPU?如果在model.cuda()所有模型参数之后有一个从nn.Module派生的自定义模块,(model.parameters()iterator可以显示这些)将在cuda上结束 要检查参数的位置,请在我的示例中打印它们(cuda:0): class M(nn.Module): 'custom module'

如果我在pytorch中调用
model.cuda()
,其中model是
nn.Module
的子类,并说如果我有四个GPU,它将如何利用这四个GPU,以及如何知道使用的是哪个GPU?

如果在
model.cuda()
所有模型参数之后有一个从
nn.Module
派生的自定义模块,(
model.parameters()
iterator可以显示这些)将在cuda上结束

要检查参数的位置,请在我的示例中打印它们(cuda:0):

class M(nn.Module):
    'custom module'
    def __init__(self):
        super().__init__()
        self.lin = nn.Linear(784, 10)

m = M()
m.cuda()
for _ in m.parameters():
    print(_)

# Parameter containing:
# tensor([[-0.0201,  0.0282, -0.0258,  ...,  0.0056,  0.0146,  0.0220],
#         [ 0.0098, -0.0264,  0.0283,  ...,  0.0286, -0.0052,  0.0007],
#         [-0.0036, -0.0045, -0.0227,  ..., -0.0048, -0.0003, -0.0330],
#         ...,
#         [ 0.0217, -0.0008,  0.0029,  ..., -0.0213,  0.0005,  0.0050],
#         [-0.0050,  0.0320,  0.0013,  ..., -0.0057, -0.0213,  0.0045],
#         [-0.0302,  0.0315,  0.0356,  ...,  0.0259,  0.0166, -0.0114]],
#        device='cuda:0', requires_grad=True)
# Parameter containing:
# tensor([-0.0027, -0.0353, -0.0349, -0.0236, -0.0230,  0.0176, -0.0156,  0.0037,
#          0.0222, -0.0332], device='cuda:0', requires_grad=True) 
您还可以按如下方式指定设备:

m.cuda('cuda:0')
使用
torch.cuda.device\u count()
您可以检查您有多少台设备。

若要扩展到在多个GPU之间分割计算,您应该使用或
分布式数据并行