# Error using CUDA

**URL:** <https://discuss.pytorch.org/t/error-using-cuda/146181>\
**Category:** vision\
**Created:** [March 11, 2022, 3:14pm UTC](https://discuss.pytorch.org/t/error-using-cuda/146181 "2022-03-11T15:14:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moshe\_Peleg](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/moshe_peleg/32/44650_2.png) [@Moshe\_Peleg](https://discuss.pytorch.org/u/Moshe_Peleg)\
**Post date:** [March 11, 2022, 3:14pm UTC](https://discuss.pytorch.org/t/error-using-cuda/146181/1 "2022-03-11T15:14:05Z")

</div>

Hi everyone!  
i am a beginner.  
i am trying to train this network (from Udacity course - public repository) on google Colab  
at first i have trained it regular.  
but then i have try using GPU to see it its actually faster

for this i have used : (each one where needed)  
“device = torch.device(“cuda” if torch.cuda.is\_available() else “cpu”)”  
“model.to(device)”  
“data.to(device), target.to(device)”

i got an error - please see after the attached code  
any help would be appreciated.

######################################################################  
#the code:

# import libraries

import torch

import numpy as np

from six.moves import urllib

opener = urllib.request.build\_opener()

opener.addheaders = [(‘User-agent’, ‘Mozilla/5.0’)]

urllib.request.install\_opener(opener)

from torchvision import datasets

import torchvision.transforms as transforms

# number of subprocesses to use for data loading

num\_workers = 0

# how many samples per batch to load

batch\_size = 20

# convert data to torch.FloatTensor

transform = transforms.ToTensor()

# choose the training and test datasets

train\_data = datasets.MNIST(root=‘data’, train=True,

```
                               download=True, transform=transform)

```

test\_data = datasets.MNIST(root=‘data’, train=False,

```
                              download=True, transform=transform)

```

# prepare data loaders

train\_loader = torch.utils.data.DataLoader(train\_data, batch\_size=batch\_size,

```
num_workers=num_workers)

```

test\_loader = torch.utils.data.DataLoader(test\_data, batch\_size=batch\_size,

```
num_workers=num_workers)

```

import torch.nn as nn

import torch.nn.functional as F

## TODO: Define the NN architecture

class Net(nn.Module):

```
def __init__ (self):

    super(Net, self). __init__ ()

    # linear layer (784 -> 10 hidden node)

    self.fc1 = nn.Linear(28 * 28, 512)

    self.fc2 = nn.Linear(512, 512)

    self.fc3 = nn.Linear(512, 10)

    

    self.dropout = nn.Dropout(0.2)

    

def forward(self, x):

    # flatten image input

    #x = x.view(-1, 28 * 28)

    x.view(x.shape[0], -1)

    # add hidden layer, with relu activation function

    x = self.dropout(F.relu(self.fc1(x)))

    x = self.dropout(F.relu(self.fc2(x)))

    x = F.relu(self.fc3(x))

    

    return x

```

# initialize the NN

# Use GPU if it’s available

device = torch.device(“cuda” if torch.cuda.is\_available() else “cpu”)

model = Net()

print(model)

model.to(device) #but i get errors!!! and i dont know why!

# specify loss function

criterion = nn.CrossEntropyLoss()

# specify optimizer

optimizer = torch.optim.Adam(model.parameters(), lr= 0.003)

# number of epochs to train the model

n\_epochs = 30 # suggest training between 20-50 epochs

# connect model to device

#model.train() # prep model for training

for epoch in range(n\_epochs):

```
# monitor training loss

train_loss = 0.0

###################

# train the model #

###################

for data, target in train_loader:

    #connect data and target to device:

    data.to(device), target.to(device)

    # clear the gradients of all optimized variables

    optimizer.zero_grad()

    # forward pass: compute predicted outputs by passing inputs to the model

    output = model(data)

    # calculate the loss

    loss = criterion(output, target)

    # backward pass: compute gradient of the loss with respect to model parameters

    loss.backward()

    # perform a single optimization step (parameter update)

    optimizer.step()

    # update running training loss

    train_loss += loss.item()*data.size(0)

    

# print training statistics 

# calculate average loss over an epoch

train_loss = train_loss/len(train_loader.dataset)

print('Epoch: {} \tTraining Loss: {:.6f}'.format(

    epoch+1, 

    train_loss

    ))

```

RuntimeError Traceback (most recent call last)  
 in ()  
24 optimizer.zero\_grad()  
25 # forward pass: compute predicted outputs by passing inputs to the model  
—\> 26 output = model(data)  
27 # calculate the loss  
28 loss = criterion(output, target)

4 frames  
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)  
1846 if has\_torch\_function\_variadic(input, weight, bias):  
1847 return handle\_torch\_function(linear, (input, weight, bias), input, weight, bias=bias)  
 → 1848 return torch.\_C.\_nn.linear(input, weight, bias)  
1849  
1850

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat2 in method wrapper\_mm)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 11, 2022, 7:40pm UTC](https://discuss.pytorch.org/t/error-using-cuda/146181/2 "2022-03-11T19:40:21Z")

</div>

The `to()` operation is not an inplace op for tensors, so you would need to reassign these tensors:

```python
data.to(device), target.to(device)

```

via:

```python
data, target = data.to(device), target.to(device)

```

---

<div class="post-metadata">

**Author:** ![Moshe\_Peleg](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/moshe_peleg/32/44650_2.png) [@Moshe\_Peleg](https://discuss.pytorch.org/u/Moshe_Peleg)\
**Post date:** [March 12, 2022, 6:36pm UTC](https://discuss.pytorch.org/t/error-using-cuda/146181/3 "2022-03-12T18:36:17Z")

</div>

thank you @ptrblck - your answer solved the issue
