Python 从GPU中的60分钟闪电战中训练PyTorch分类器时出错
我已经开始在jupyter实验室学习Pytork的官方60分钟blitz教程(使用他们的.ipynb文件),并成功地完成了它,直到使用gpu转换和训练分类器。我认为我已经根据这些结果成功地更改了网络、输入和标签的设备:Python 从GPU中的60分钟闪电战中训练PyTorch分类器时出错,python,pytorch,jupyter-lab,Python,Pytorch,Jupyter Lab,我已经开始在jupyter实验室学习Pytork的官方60分钟blitz教程(使用他们的.ipynb文件),并成功地完成了它,直到使用gpu转换和训练分类器。我认为我已经根据这些结果成功地更改了网络、输入和标签的设备: net=net.to(device) net.fc1.weight.type() 输出: 'torch.cuda.FloatTensor' 以及: 输出: ('torch.cuda.FloatTensor', 'torch.cuda.LongTensor') 运行这些单元格
net=net.to(device)
net.fc1.weight.type()
输出:
'torch.cuda.FloatTensor'
以及:
输出:
('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'
运行时错误回溯(最近一次调用)
在()
10
11#向前+向后+优化
--->12输出=净(输入)
13损耗=标准(输出、标签)
14.损失向后()
~\Anaconda3\lib\site packages\torch\nn\modules\module.py在调用中(self,
*输入,**千瓦格)
475结果=self.\u slow\u forward(*输入,**kwargs)
476其他:
-->477结果=自我转发(*输入,**kwargs)
478用于钩住自身。\u向前\u钩住.values():
479钩子结果=钩子(自身、输入、结果)
前进中(自我,x)
14
15 def前进档(自身,x):
--->16 x=自转换1(x)
17 x=自池(F.relu(x))
18 x=自池(F.relu(self.conv2(x)))
~\Anaconda3\lib\site packages\torch\nn\modules\module.py在调用中(self,
*输入,**千瓦格)
475结果=self.\u slow\u forward(*输入,**kwargs)
476其他:
-->477结果=自我转发(*输入,**kwargs)
478用于钩住自身。\u向前\u钩住.values():
479钩子结果=钩子(自身、输入、结果)
~\Anaconda3\lib\site packages\torch\nn\modules\conv.py在前进(self,
输入)
299 def前进档(自身,输入):
300返回F.conv2d(输入、自重、自偏倚、自步幅、,
-->301自填充、自膨胀、自组)
302
303
RuntimeError:应为torch.FloatTensor类型的对象,但找到类型为
torch.cuda.FloatTensor用于参数#2‘重量’
我为什么会收到此错误,如何修复它 您还需要将
输入
和标签
移动到训练循环内的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)
...
更准确地说,您需要移动每一批训练数据。
for i, data in enumerate(trainloader, 0):
# get the inputs
inputs, labels = data
# move to GPU
inputs = inputs.to(device)
labels = labels.to(device)
...