我已经开始在Jupyter实验室中使用他们的官方60分钟闪电战教程来学习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'
为什么会收到此错误,我该如何解决?
答案 0 :(得分:0)
您还需要在训练循环内将inputs
和labels
移动到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)
...