【问题标题】:Error in training PyTorch classifier from the 60 minute blitz in GPU从 GPU 的 60 分钟闪电战中训练 PyTorch 分类器时出错
【发布时间】:2018-11-01 17:29:10
【问题描述】:

我已经开始在 jupyter 实验室中通过他们的 60 分钟 blitz 官方教程学习 pytorch(使用他们的 .ipynb 文件,link to the tutorial),并成功完成了它,直到使用 gpu 转换和训练分类器。我认为我已经设法根据这些结果更改了网络、输入和标签的设备:

net=net.to(device)
net.fc1.weight.type()

有输出:

'torch.cuda.FloatTensor'

还有:

inputs, labels = inputs.to(device), labels.to(device)
inputs.type(),labels.type()

有输出:

('torch.cuda.FloatTensor', 'torch.cuda.LongTensor')

运行这些单元后,我运行了用于训练模型的单元,其中包含以下代码:

for epoch in range(2):  # loop over the dataset multiple times

running_loss = 0.0
for i, data in enumerate(trainloader, 0):
    # get the inputs
    inputs, labels = data

    # zero the parameter gradients
    optimizer.zero_grad()

    # forward + backward + optimize
    outputs = net(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()

    # print statistics
    running_loss += loss.item()
    if i % 2000 == 1999:    # print every 2000 mini-batches
        print('[%d, %5d] loss: %.3f' %
              (epoch + 1, i + 1, running_loss / 2000))
        running_loss = 0.0

print('Finished Training') 

并收到此错误:

RuntimeError                              Traceback (most recent call last)
<ipython-input-55-fe85c778b0e6> in <module>()
     10 
     11         # forward + backward + optimize
---> 12         outputs = net(inputs)
     13         loss = criterion(outputs, labels)
     14         loss.backward()

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, 
*input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

<ipython-input-52-725d44154459> in forward(self, x)
    14 
    15     def forward(self, x):
--->16         x=self.conv1(x)
    17         x = self.pool(F.relu(x))
    18         x = self.pool(F.relu(self.conv2(x)))

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, 
*input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, 
input)
    299     def forward(self, input):
    300         return F.conv2d(input, self.weight, self.bias, self.stride,
--> 301                         self.padding, self.dilation, self.groups)
    302 
    303 

RuntimeError: Expected object of type torch.FloatTensor but found type 
torch.cuda.FloatTensor for argument #2 'weight' 

为什么我会收到此错误,我该如何解决?

【问题讨论】:

    标签: python pytorch jupyter-lab


    【解决方案1】:

    您还需要将 inputslabels 移动到训练循环内的 GPU。

    for i, data in enumerate(trainloader, 0):
        # get the inputs
        inputs, labels = data
    
        # move to GPU
        inputs = inputs.to(device)
        labels = labels.to(device)
    
        ...
    

    【讨论】:

    • 更准确地说,您需要移动每批训练数据。
    猜你喜欢
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2019-01-24
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    相关资源
    最近更新 更多