pytorch - net.cuda() 似乎不起作用

Dan*_*hen 3 python pytorch

我编写了一个 cnn 模块来使用 pytorch 进行数字识别,然后尝试使用 GPU 训练网络,但出现以下错误。

Traceback (most recent call last):
  File "main.py", line 51, in <module>
    outputs = cnn(inputs)
  File "/home/daniel/anaconda3/envs/pytorch/lib/python3.5/site-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/daniel/Code/kaggle-competitions/digit-recognizer/Net.py", line 40, in forward
    x = self.pool(F.relu(self.conv[i](x)))
  File "/home/daniel/anaconda3/envs/pytorch/lib/python3.5/site-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/daniel/anaconda3/envs/pytorch/lib/python3.5/site-packages/torch/nn/modules/conv.py", line 282, in forward
    self.padding, self.dilation, self.groups)
  File "/home/daniel/anaconda3/envs/pytorch/lib/python3.5/site-packages/torch/nn/functional.py", line 90, in conv2d
    return f(input, weight, bias)
RuntimeError: Input type (CUDAFloatTensor) and weight type (CPUFloatTensor) should be the same
Run Code Online (Sandbox Code Playgroud)

这是我的源代码

似乎cnn.cuda()无法正常工作,因为删除它后我遇到了同样的错误。但我不知道如何解决它。

小智 5

丹尼尔对自己问题的回答似乎是正确的。问题确实是,如果将模块附加到列表中,则无法识别它们。然而,Pytorch 也为这个问题提供了内置的解决方案: nn.ModuleListnn.ModuleDict是两种容器类型,用于跟踪添加的内容及其参数。两者具有与 Python 等效项相同的功能,但字典使用命名参数,并且可用于跟踪特定于任务的层等。

一个有效的例子是:

    self.conv = torch.nn.ModuleList()
    self.conv.append(nn.Conv2d(1, channels[0], kernel_sizes[0]))
    self.conv_img_size = math.floor((self.conv_img_size - (kernel_sizes[0]-1))/2)

    for i in range(1, self.conv_layer_size):
        self.conv.append(nn.Conv2d(channels[i-1], channels[i], kernel_sizes[i]))
        self.conv_img_size = math.floor((self.conv_img_size - (kernel_sizes[i]-1))/2)
        # Modules are automatically added to the model parameters
Run Code Online (Sandbox Code Playgroud)