Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a tensor is on cuda or send it to cuda in Pytorch?

I have a tensor

t = torch.zeros((4, 5, 6))

How to check if it is on gpu or not, and send it to gpu and back?

like image 990
Gulzar Avatar asked Sep 08 '25 04:09

Gulzar


1 Answers

From the pytorch forum

use t.is_cuda, t.cuda(), t.cpu()

t = torch.randn(2,2)
t.is_cuda  # returns False
t = torch.randn(2,2).cuda()
t.is_cuda  # returns True
t = t.cpu()
t.is_cuda  # returns False

When passing to and from gpu and cpu, new arrays are allocated on the relevant device.

like image 131
Gulzar Avatar answered Sep 10 '25 00:09

Gulzar