Hello,
I am new to pytorch and trying to understand it. When I see a codes written in pytorch, to utilize GPU sometimes .cuda() is used while .to(‘cuda’) is used sometimes. I want to know if there is any difference between both methods or both are same?
Hi,
There are the same. .cuda() is the old api and .to() is a more recent api that can take any device and/or type and so more general.
.cuda()
.to()
Their syntax varies slightly, but they are equivalent:
to('cuda')
cuda()
to('cuda:1')
cuda(device=1)
to('cpu')
cpu()
Note: the first row uses the default GPU (this can be set with torch.cuda.set_device()).
torch.cuda.set_device()