GPU sending 'weight' backend error

Jordan_Howell: <p>I’m getting an error that states the expected object of backend CPU but got CUDA for argument <span class="hashtag-raw">#2</span> ‘weight’.  I’m not sure what that means and the full traceback is below.</p>
<blockquote>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
25<br>
26         # Apply the model<br>
—&gt; 27         y_pred = CNNmodel(X_train)<br>
28         loss = criterion(y_pred, y_train)<br>
29</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
545             result = self._slow_forward(*input, **kwargs)<br>
546         else:<br>
 → 547             result = self.forward(*input, **kwargs)<br>
548         for hook in self._forward_hooks.values():<br>
549             hook_result = hook(self, input, result)</p>
<p> in forward(self, X)<br>
9<br>
10     def forward(self, X):<br>
—&gt; 11         X = F.relu(self.conv1(X))<br>
12         X = F.max_pool2d(X, 2, 2)<br>
13         X = F.relu(self.conv2(X))</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
545             result = self._slow_forward(*input, **kwargs)<br>
546         else:<br>
 → 547             result = self.forward(*input, **kwargs)<br>
548         for hook in self._forward_hooks.values():<br>
549             hook_result = hook(self, input, result)</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)<br>
341<br>
342     def forward(self, input):<br>
 → 343         return self.conv2d_forward(input, self.weight)<br>
344<br>
345 class Conv3d(_ConvNd):</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in conv2d_forward(self, input, weight)<br>
338                             _pair(0), self.dilation, self.groups)<br>
339         return F.conv2d(input, weight, self.bias, self.stride,<br>
 → 340                         self.padding, self.dilation, self.groups)<br>
341<br>
342     def forward(self, input):</p>
<p>RuntimeError: Expected object of backend CPU but got backend CUDA for argument <span class="hashtag-raw">#2</span> ‘weight’</p>
</blockquote>
<p>After developing the model class, I send the model to the GPU via the following command:</p>
<blockquote>
<p>CNNmodel = ConvolutionalNetwork().to(device)</p>
</blockquote>
<p>And I’m not sure if this is needed but here is the train/test data set preperation:</p>
<blockquote>
<p>full_data = datasets.ImageFolder(os.path.join(root, ‘train’), transform=train_transform)<br>
train_size = int(0.8 * len(full_data))<br>
test_size = len(full_data) - train_size</p>
<p>train_dataset, test_dataset = torch.utils.data.random_split(full_data, [train_size, test_size])</p>
<p>torch.manual_seed(42)<br>
train_loader = DataLoader(train_dataset, batch_size=10, shuffle=True)<br>
test_loader = DataLoader(test_dataset, batch_size=10, shuffle=True)</p>
<p>class_names = full_data.classes</p>
<p>print(class_names)<br>
print(f’Training images available: {len(train_dataset)}‘)<br>
print(f’Testing images available:  {len(test_dataset)}’)</p>
</blockquote>
<p>Finally, here is my training script:</p>
<blockquote>
<p>import time<br>
start_time = time.time()</p>
<p>epochs = 63</p>
<p>max_trn_batch = 1000<br>
max_tst_batch = 300</p>
<p>train_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
test_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
train_correct = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
test_correct = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>for i in range(epochs):<br>
trn_corr = 0<br>
tst_corr = 0</p>
<pre><code># 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}  \
</code></pre>
<p>accuracy: {trn_corr.item()<em>100/(10</em>b):7.3f}%')</p>
<pre><code>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)
</code></pre>
<p>print(f’\nDuration: {time.time() - start_time:.0f} seconds’) # print the time elapsed</p>
</blockquote>
<p>Am I missing sending something else to the GPU?</p>

albanD: <p>Hi,</p>
<p>I think you are not sending <code>X_train</code> and <code>y_train</code> to the GPU.</p>