Input Tensors not being moved to GPU in pytorch
When running my code, I get the error:

Input and parameter tensors are not at the same device, found input tensor at cpu and parameter tensor at cuda:0

even though I'm using .cuda() on my inputs.

Google Colab link

Code:

use_cuda = True
if use_cuda and torch.cuda.is_available():
   model.cuda()

def test():
model.eval()
avgLoss = 0
for dataPoint in range(len(testData)):
    lstmInput = testData[dataPoint][0]
    lstmInput = torch.Tensor(lstmInput)
    lstmInput = lstmInput.view(len(testData[dataPoint][0]), 1, 5)
    label = testData[dataPoint][1]
    label = torch.Tensor(label)
    lstmInput = Variable(lstmInput)
    label = Variable(label)

    if use_cuda and torch.cuda.is_available():
          lstmInput.cuda()
          label.cuda()

    pred_label = model(lstmInput)
    loss = loss_fn(label, pred_label)
    avgLoss += loss.item()
return avgLoss / len(testData)

def train(num_epochs):
model.train()
for epoch in range(num_epochs):
    avgLoss = 0.0
    for datapoint in range(len(trainData)):
        model.hidden = model.init_hidden()
        optimizer.zero_grad()

        lstmInput = trainData[datapoint][0]
        lstmInput = torch.Tensor(lstmInput)
        lstmInput = lstmInput.view(len(trainData[datapoint][0]), 1, 5)
        label = torch.Tensor(trainData[datapoint][1])
        label = label.view(1, 5)
        lstmInput = Variable(lstmInput)
        label = Variable(label)

        if use_cuda and torch.cuda.is_available():
          print("happens")
          lstmInput.cuda()
          label.cuda()

        pred_label = model(lstmInput)
        loss = loss_fn(pred_label, label)
        # print(label, pred_label)
        avgLoss += loss.item()
        loss.backward()
        optimizer.step()
    print("Epoch: ", epoch, "MSELoss: ", avgLoss / len(trainData), "Test Acc: ", test())


The cuda()method returns the tensor on the right gpu so you need to assign it back to your input variable:

lstmInput, label = lstimInput.cuda(), label.cuda()

