GPU sending 'weight' backend error

I’m getting an error that states the expected object of backend CPU but got CUDA for argument #2 ‘weight’. I’m not sure what that means and the full traceback is below.


RuntimeError Traceback (most recent call last)
in
25
26 # Apply the model
—> 27 y_pred = CNNmodel(X_train)
28 loss = criterion(y_pred, y_train)
29

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)

in forward(self, X)
9
10 def forward(self, X):
—> 11 X = F.relu(self.conv1(X))
12 X = F.max_pool2d(X, 2, 2)
13 X = F.relu(self.conv2(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’

After developing the model class, I send the model to the GPU via the following command:

CNNmodel = ConvolutionalNetwork().to(device)

And I’m not sure if this is needed but here is the train/test data set preperation:

full_data = datasets.ImageFolder(os.path.join(root, ‘train’), transform=train_transform)
train_size = int(0.8 * len(full_data))
test_size = len(full_data) - train_size

train_dataset, test_dataset = torch.utils.data.random_split(full_data, [train_size, test_size])

torch.manual_seed(42)
train_loader = DataLoader(train_dataset, batch_size=10, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=10, shuffle=True)

class_names = full_data.classes

print(class_names)
print(f’Training images available: {len(train_dataset)}’)
print(f’Testing images available: {len(test_dataset)}’)

Finally, here is my training script:

import time
start_time = time.time()

epochs = 63

max_trn_batch = 1000
max_tst_batch = 300

train_losses = []
test_losses = []
train_correct = []
test_correct = []

for i in range(epochs):
trn_corr = 0
tst_corr = 0

# Run the training batches
for b, (X_train, y_train) in enumerate(train_loader):
    
    # Limit the number of batches
    if b == max_trn_batch:
        break
    b+=1
    
    # Apply the model
    y_pred = CNNmodel(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%200 == 0:
        print(f'epoch: {i:2}  batch: {b:4} [{10*b:6}/8000]  loss: {loss.item():10.8f}  \

accuracy: {trn_corr.item()100/(10b):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) in enumerate(test_loader):
        # Limit the number of batches
        if b == max_tst_batch:
            break

        # Apply the model
        y_val = CNNmodel(X_test)

        # 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

Am I missing sending something else to the GPU?

Hi,

I think you are not sending X_train and y_train to the GPU.