gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:17:27 25 4
gpt4 key购买 nike

我编写了一个 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

这是我的source code

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

最佳答案

丹尼尔对自己问题的回答似乎是正确的。问题确实是,如果将模块附加到列表中,则无法识别它们。不过,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

关于python - pytorch - net.cuda() 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com