Tensor.cuda() vs Tensor.to('cuda')

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?

3 Likes

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.

1 Like

Their syntax varies slightly, but they are equivalent:

.to() .cuda()
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()).

2 Likes