talha06
talha06

Reputation: 6466

PyTorch - GPU is not used by tensors despite CUDA support is detected

As the title of the question clearly describes, even though torch.cuda.is_available() returns True, CPU is used instead of GPU by tensors. I have set the device of the tensor to GPU through the images.to(device) function call after defining the device. When I debug my code, I am able to see that the device is set to cuda:0; but the tensor's device is still set to cpu.

Defining the device:

use_cuda = torch.cuda.is_available()  # returns True
device = torch.device('cuda:0' if use_cuda else 'cpu')

Determining the device of the tensors:

for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images.to(device)
        labels.to(device)
        # both of images and labels' devices are set to cpu

The software stack:

Python 3.7.1
torch 1.0.1
Windows 10 64-bit

p.s. PyTorch is installed with the option of Cuda 9.0 support.

Upvotes: 1

Views: 788

Answers (1)

Haran Rajkumar
Haran Rajkumar

Reputation: 2395

tensor.to() does not modify the tensor inplace. It returns a new tensor that's stored in the specified device.

Use the following instead.

 images = images.to(device)
 labels = labels.to(device)

Upvotes: 3

Related Questions