Is there a way in Pytorch to check if something is a number(dim()==0) or a tensor(dim() >0)

How can I know I’m dealing with a number or a tensor other than checking the dim() method?
is there a method for this? or should I just use dim() for this?
Thank you all in advance

i think type(variable) is what you want

1 Like

using type only results in <class 'torch.Tensor'> for everything in this case.

I agree with @cyanM
the alternative would be using torch.is_tensor()

1 Like

Thanks, I have sth like :

if one_hot_tensor.size(0) > 1 : 
    return torch.argmax(one_hot_tensor)
    return one_hot_tensor[0].int()

and this always returns a tensor and none of these proposed methods work!

it means that one_hot_tensor.size(0) > 1 has always been true, and torch.argmax() will return a tensor

1 Like