I have followed this link and this one too. But the error persists.
I'm trying a simple feedforward NN using pytorch (from a tutorial).
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transformers
# hyperparameters
input_size = 784
hidden_size = 100
num_classes = 10
num_epochs = 2
learning_rate = 0.001
batch_size = 100
# activating device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Loading data
train_dataset = torchvision.datasets.MNIST('.', train=True, transform=transformers.ToTensor(), download=True)
test_dataset = torchvision.datasets.MNIST('.', train=False, transform=transformers.ToTensor(), download=False)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=False)
class FeedforwardNN(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(FeedforwardNN, self).__init__()
self.l1 = nn.Linear(input_size, hidden_size)
self.r1 = nn.ReLU()
self.l2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
out = self.l1(x)
out = self.r1(out)
out = self.l2(out)
return out
model = FeedforwardNN(input_size, hidden_size, num_classes)
model.to(device) # Added to solve the mentioned error, but to no avail.
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Training Loop
n_total_steps = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.reshape(-1, 28*28).to(device)
y_pred = model(images)
loss = criterion(y_pred, labels)
loss.backward()
optimizer.zero_grad()
I get the following error
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument target in method wrapper_nll_loss_forward)
I tried adding to(device)
on criterion
and optimizer
. But that doesn't resolve the error.
The tutorial that uses the same code seems to run properly.
At the time of this writing cuda
is inactive. Although that shouldn't matter as we had given an if
condition, that will then choose cpu
>>> torch.cuda.is_available()
False
Where should be the error then ?