Error using CUDA

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)

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

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

via:

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

thank you @ptrblck - your answer solved the issue