Error using CUDA

Moshe_Peleg: <p>Hi everyone!<br>
i am a beginner.<br>
i am trying to train this network (from Udacity course - public repository) on google Colab<br>
at first i have trained it regular.<br>
but then i have try using GPU to see it its actually faster</p>
<p>for this i have used : (each one where needed)<br>
“device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)”<br>
“model.to(device)”<br>
“data.to(device), target.to(device)”</p>
<p>i got an error - please see after the attached code<br>
any help would be appreciated.</p>
<p>######################################################################<br>
<span class="hashtag">#the</span> code:</p>
<h1>
<a name="import-libraries-1" class="anchor" href="#import-libraries-1"></a>import libraries</h1>
<p>import torch</p>
<p>import numpy as np</p>
<p>from six.moves import urllib</p>
<p>opener = urllib.request.build_opener()</p>
<p>opener.addheaders = [(‘User-agent’, ‘Mozilla/5.0’)]</p>
<p>urllib.request.install_opener(opener)</p>
<p>from torchvision import datasets</p>
<p>import torchvision.transforms as transforms</p>
<h1>
<a name="number-of-subprocesses-to-use-for-data-loading-2" class="anchor" href="#number-of-subprocesses-to-use-for-data-loading-2"></a>number of subprocesses to use for data loading</h1>
<p>num_workers = 0</p>
<h1>
<a name="how-many-samples-per-batch-to-load-3" class="anchor" href="#how-many-samples-per-batch-to-load-3"></a>how many samples per batch to load</h1>
<p>batch_size = 20</p>
<h1>
<a name="convert-data-to-torchfloattensor-4" class="anchor" href="#convert-data-to-torchfloattensor-4"></a>convert data to torch.FloatTensor</h1>
<p>transform = transforms.ToTensor()</p>
<h1>
<a name="choose-the-training-and-test-datasets-5" class="anchor" href="#choose-the-training-and-test-datasets-5"></a>choose the training and test datasets</h1>
<p>train_data = datasets.MNIST(root=‘data’, train=True,</p>
<pre><code>                               download=True, transform=transform)
</code></pre>
<p>test_data = datasets.MNIST(root=‘data’, train=False,</p>
<pre><code>                              download=True, transform=transform)
</code></pre>
<h1>
<a name="prepare-data-loaders-6" class="anchor" href="#prepare-data-loaders-6"></a>prepare data loaders</h1>
<p>train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size,</p>
<pre><code>num_workers=num_workers)
</code></pre>
<p>test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size,</p>
<pre><code>num_workers=num_workers)
</code></pre>
<p>import torch.nn as nn</p>
<p>import torch.nn.functional as F</p>
<h2>
<a name="todo-define-the-nn-architecture-7" class="anchor" href="#todo-define-the-nn-architecture-7"></a>TODO: Define the NN architecture</h2>
<p>class Net(nn.Module):</p>
<pre><code>def __init__(self):

    super(Net, self).__init__()

    # linear layer (784 -&gt; 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
</code></pre>
<h1>
<a name="initialize-the-nn-8" class="anchor" href="#initialize-the-nn-8"></a>initialize the NN</h1>
<h1>
<a name="use-gpu-if-its-available-9" class="anchor" href="#use-gpu-if-its-available-9"></a>Use GPU if it’s available</h1>
<p>device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)</p>
<p>model = Net()</p>
<p>print(model)</p>
<p>model.to(device) <span class="hashtag">#but</span> i get errors!!! and i dont know why!</p>
<h1>
<a name="specify-loss-function-10" class="anchor" href="#specify-loss-function-10"></a>specify loss function</h1>
<p>criterion = nn.CrossEntropyLoss()</p>
<h1>
<a name="specify-optimizer-11" class="anchor" href="#specify-optimizer-11"></a>specify optimizer</h1>
<p>optimizer = torch.optim.Adam(model.parameters(), lr= 0.003)</p>
<h1>
<a name="number-of-epochs-to-train-the-model-12" class="anchor" href="#number-of-epochs-to-train-the-model-12"></a>number of epochs to train the model</h1>
<p>n_epochs = 30  # suggest training between 20-50 epochs</p>
<h1>
<a name="connect-model-to-device-13" class="anchor" href="#connect-model-to-device-13"></a>connect model to device</h1>
<p><span class="hashtag">#model</span>.train() # prep model for training</p>
<p>for epoch in range(n_epochs):</p>
<pre><code># 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

    ))
</code></pre>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
24         optimizer.zero_grad()<br>
25         # forward pass: compute predicted outputs by passing inputs to the model<br>
—&gt; 26         output = model(data)<br>
27         # calculate the loss<br>
28         loss = criterion(output, target)</p>
<p>4 frames<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)<br>
1846     if has_torch_function_variadic(input, weight, bias):<br>
1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)<br>
 → 1848     return torch._C._nn.linear(input, weight, bias)<br>
1849<br>
1850</p>
<p>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)</p>

ptrblck: <p>The <code>to()</code> operation is not an inplace op for tensors, so you would need to reassign these tensors:</p>
<pre><code class="lang-python">data.to(device), target.to(device)
</code></pre>
<p>via:</p>
<pre><code class="lang-python">data, target = data.to(device), target.to(device)
</code></pre>

Moshe_Peleg: <p>thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> - your answer solved the issue</p>