Rylan Schaeffer
Rylan Schaeffer

Reputation: 2705

Configure PyCharm Debugger to Display Array/Tensor Shape?

When I'm debugging with PyCharm, I'd like for the debugger to display the shapes of my NumPy arrays / Jax arrays / PyTorch Tensors. Instead, I see their values:

enter image description here

Is there a way to configure PyCharm's debugger so that the shapes of these multi-dimensional arrays is displayed?

Upvotes: 3

Views: 2311

Answers (2)

LI Qimai
LI Qimai

Reputation: 106

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: PyCharm Debugger

Upvotes: 9

Andrey Resler
Andrey Resler

Reputation: 311

Debugger just shows the objects in memory. So if you create an object representing shape, you should see it in the debugger.

For example, for numpy it's numpy.array().shape

Upvotes: 0

Related Questions