0
votes

I'm learning PyTorch recently, and this question comes up. For example, if I have a net inheriting the "torch.nn.Module".

class Net(torch.nn.Module):
    def __init__(self, something):
        super(net, self).__init__()
        self.p1=something

    def forward():
        pass

net1=Net(123)
net1.cuda()  ##Here I can't see what is changed.

Then how can I know whether net1 (and that something) is stored on GPU.

I've read how the *.cuda() works, seems like let all the "children" run the *.cuda(). I tried to see what the "children" are. It seems the net1 above has no children.

1

1 Answers

3
votes

To check a simple tensor, you can check the is_cuda attribute. For example:

x = torch.zeros(100).cuda()
y = torch.zeros(100)

print(x.is_cuda) # True
print(y.is_cuda) # False

To check a model, a think the easiest way is using the parameters() method, which returns all trainable parameters of your model.

next(model.parameters()).is_cuda