Reputation: 6466
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
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