为什么测试集上出现Pytorch运行时错误
我有一个模型,它是一个带有resnext模型的二值图像分类模型。当运行时错误到达测试集时,我不断收到它。错误消息是为什么测试集上出现Pytorch运行时错误,pytorch,Pytorch,我有一个模型,它是一个带有resnext模型的二值图像分类模型。当运行时错误到达测试集时,我不断收到它。错误消息是 RuntimeError:预期的对象是后端CPU,但参数为后端CUDA#2“权重” 我将测试集张量发送到我的GPU,就像我的火车模型一样。我已经看了下面的内容,我正在做上面提到的建议 这是我的型号代码: resnext = models.resnext50_32x4d(pretrained=True) resnext = resnext.to(device) for param i
RuntimeError:预期的对象是后端CPU,但参数为后端CUDA#2“权重”
我将测试集张量发送到我的GPU,就像我的火车模型一样。我已经看了下面的内容,我正在做上面提到的建议
这是我的型号代码:
resnext = models.resnext50_32x4d(pretrained=True)
resnext = resnext.to(device)
for param in resnext.parameters():
param.requires_grad = True
resnext.classifier = nn.Sequential(nn.Linear(2048, 1000),
nn.ReLU(),
nn.Dropout(0.4),
nn.Linear(1000, 2),
nn.Softmax(dim = 1))
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(resnext.classifier.parameters(), lr=0.001)
import time
start_time = time.time()
epochs = 1
max_trn_batch = 5
max_tst_batch = 156
y_val_list = []
policy_list = []
train_losses = []
test_losses = []
train_correct = []
test_correct = []
for i in range(epochs):
for i in tqdm(range(0, max_trn_batch)):
trn_corr = 0
tst_corr = 0
# Run the training batches
for b, (X_train, y_train, policy) in enumerate(train_loader):
#print(y_train, policy)
X_train = X_train.to(device)
y_train = y_train.to(device)
if b == max_trn_batch:
break
b+=1
# Apply the model
y_pred = resnext(X_train)
loss = criterion(y_pred, y_train)
# Tally the number of correct predictions
predicted = torch.max(y_pred.data, 1)[1]
batch_corr = (predicted == y_train).sum()
trn_corr += batch_corr
# Update parameters
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print interim results
if b%1 == 0:
print(f'epoch: {i:2} batch: {b:4} [{100*b:6}/63610] loss: {loss.item():10.8f} \
accuracy: {trn_corr.item()/(100*b):7.3f}%')
train_losses.append(loss)
train_correct.append(trn_corr)
# Run the testing batches
with torch.no_grad():
for b, (X_test, y_test, policy) in enumerate(test_loader):
policy_list.append(policy)
X_test.to(device)
y_test.to(device)
if b == max_tst_batch:
break
# Apply the model
y_val = resnext(X_test)
y_val_list.append(y_val.data)
# Tally the number of correct predictions
predicted = torch.max(y_val.data, 1)[1]
tst_corr += (predicted == y_test).sum()
loss = criterion(y_val, y_test)
test_losses.append(loss)
test_correct.append(tst_corr)
print(f'\nDuration: {time.time() - start_time:.0f} seconds') # print the time elapsed
以下是完整的回溯:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-84-48bce2e8d4fa> in <module>
60
61 # Apply the model
---> 62 y_val = resnext(X_test)
63 y_val_list.append(y_val.data)
64 # Tally the number of correct predictions
C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
545 result = self._slow_forward(*input, **kwargs)
546 else:
--> 547 result = self.forward(*input, **kwargs)
548 for hook in self._forward_hooks.values():
549 hook_result = hook(self, input, result)
C:\ProgramData\Anaconda3\lib\site-packages\torchvision\models\resnet.py in forward(self, x)
194
195 def forward(self, x):
--> 196 x = self.conv1(x)
197 x = self.bn1(x)
198 x = self.relu(x)
C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
545 result = self._slow_forward(*input, **kwargs)
546 else:
--> 547 result = self.forward(*input, **kwargs)
548 for hook in self._forward_hooks.values():
549 hook_result = hook(self, input, result)
C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
341
342 def forward(self, input):
--> 343 return self.conv2d_forward(input, self.weight)
344
345 class Conv3d(_ConvNd):
C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in conv2d_forward(self, input, weight)
338 _pair(0), self.dilation, self.groups)
339 return F.conv2d(input, weight, self.bias, self.stride,
--> 340 self.padding, self.dilation, self.groups)
341
342 def forward(self, input):
RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'weight'
---------------------------------------------------------------------------
运行时错误回溯(上次最近调用)
在里面
60
61#应用模型
--->62 y_val=resnext(X_测试)
63 y值列表。追加(y值数据)
64#计算正确预测的数量
C:\ProgramData\Anaconda3\lib\site packages\torch\nn\modules\module.py in\uuuuuu调用(self,*input,**kwargs)
545结果=self.\u slow\u forward(*输入,**kwargs)
546其他:
-->547结果=自我转发(*输入,**kwargs)
548用于钩住自身。\u向前\u钩住.values():
549钩子结果=钩子(自身、输入、结果)
C:\ProgramData\Anaconda3\lib\site packages\torchvision\models\resnet.py in forward(self,x)
194
195 def前进档(自身,x):
-->196 x=自我调节1(x)
197 x=self.bn1(x)
198 x=self.relu(x)
C:\ProgramData\Anaconda3\lib\site packages\torch\nn\modules\module.py in\uuuuuu调用(self,*input,**kwargs)
545结果=self.\u slow\u forward(*输入,**kwargs)
546其他:
-->547结果=自我转发(*输入,**kwargs)
548用于钩住自身。\u向前\u钩住.values():
549钩子结果=钩子(自身、输入、结果)
C:\ProgramData\Anaconda3\lib\site packages\torch\nn\modules\conv.py in forward(self,input)
341
342 def前进档(自身,输入):
-->343返回自我控制前进(输入,自我重量)
344
345类Conv3d(_ConvNd):
C:\ProgramData\Anaconda3\lib\site packages\torch\nn\modules\conv.py in conv2d\u forward(自身、输入、重量)
338对(0),自膨胀,自组)
339返回F.conv2d(输入、重量、自我偏差、自我步幅、,
-->340自填充、自膨胀、自组)
341
342 def前进档(自身,输入):
RuntimeError:应为后端CPU的对象,但为参数#2“weight”获取了后端CUDA
同样,我的张量和模型被发送到GPU,所以我不确定发生了什么。有人看到我的错误了吗
[…]我的张量和模型被发送到GPU[…]
不是测试
张量。这是一个简单的错误:
X_测试到(设备)
y_测试到(设备)
应该是
X_test=X_test.to(设备)
y_测试=y_测试到(设备)
好吧,去死吧。谢谢@Berriel@Jordan每个人都会这样:)