I achieve this by hacking member function torch.Tensor.__repr__, because PyCharm debugger calls __repr__ to get string representation of the object.
import torch
old_repr = torch.Tensor.__repr__
def tensor_info(tensor):
return repr(tensor.shape)[6:] + ' ' + repr(tensor.dtype)[6:] + '@' + str(tensor.device) + '\n' + old_repr(tensor)
torch.Tensor.__repr__ = tensor_info
In PyCharm debugger, you will see representation like:
>>>print(torch.ones(3,3))
Size([3, 3]) float32@cpu
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Snapshot of my variable inspector:
