How to run a pre-trained pytorch model on the GPU?
Here I am trying to use the mobilenetv2 mobile to train on a custom dataset. I can get it to work on the CPU, but I would prefer to run it on the GPU. Instead, I am getting errors like these:

RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'weight'

RuntimeError: Expected object of backend CPU but got backend CUDA for argument #4 'mat1

So like my Post asks how can I get the pre-trained model to run on the GPU?

MobileNet = models.mobilenet_v2(pretrained = True)
if torch.cuda.is_available():
    MobileNet.cuda()

for param in MobileNet.parameters():
    param.requires_grad = False

    torch.manual_seed(50)

MobileNet.classifier = nn.Sequential(nn.Linear(1280, 1000), nn.ReLU(), nn.Dropout(0.5), nn.Linear(1000,3), nn.LogSoftmax(dim=1))

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(MobileNet.classifier.parameters(), lr=0.001)

train_transform = transforms.Compose([
        transforms.RandomRotation(10),      # rotate +/- 10 degrees
        transforms.RandomHorizontalFlip(),  # reverse 50% of images
        transforms.Resize(224),             # resize shortest side to 224 pixels
        transforms.CenterCrop(224),         # crop longest side to 224 pixels at center
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],
                             [0.229, 0.224, 0.225])
    ])

test_transform = transforms.Compose([
        transforms.Resize(224),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],
                             [0.229, 0.224, 0.225])
    ])

train_data = datasets.ImageFolder('C:/Users/mixv/Pictures/Summer/datasets/train', transform=train_transform)
test_data = datasets.ImageFolder('C:/Users/mix/Pictures/Summer/datasets/test', transform=test_transform)


torch.manual_seed(42)
batch=64
train_loader = DataLoader(train_data, batch_size=batch, shuffle=True)
test_loader = DataLoader(test_data, batch_size=batch, shuffle=True)

if torch.cuda.is_available():
    train_loader = DataLoader(train_data, batch_size=batch, shuffle=True, pin_memory = True)
    test_loader = DataLoader(test_data, batch_size=batch, shuffle=True, pin_memory = True)

epochs = 10

train_losses = []
test_losses = []
train_correct = []
test_correct = []
start_time =time.time()
for i in range(epochs):
    trn_corr = 0
    tst_corr = 0

    # Run the training batches

    for b, (images, labels) in enumerate(train_loader):

        if torch.cuda.is_available():
            images = images.cuda()
            labels = labels.cuda()

        b+=1

        # Apply the model
        y_pred = MobileNet(images)
        loss = criterion(y_pred, labels)

        # Tally the number of correct predictions
        predicted = torch.max(y_pred.data, 1)[1]
        batch_corr = (predicted == labels).sum()
        trn_corr += batch_corr

        accuracy = trn_corr.item()*100/(b*batch)
        # Update parameters
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()


As the RuntimeError said, some weights are still in cpu. One possible flaw I suspect is MobileNet.classifier = nn.Sequential(nn.Linear(1280, 1000), nn.ReLU(), nn.Dropout(0.5), nn.Linear(1000,3), nn.LogSoftmax(dim=1)) is done after MobileNet.cuda(), which mean these new created weight probably did not be sent to gpu. Try reverse these two's sequence and see
