5

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?

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

1 Comment

this doesn't work when the tensor is large. I see a lot of "..." in the representation
0

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

3 Comments

Debugger doesn't just show objects in memory; it chooses which attributes to display alongside the objects. I want to change the default attribute displayed so that np.array().shape is displayed next to the object without needing to "open" the object in the debugger console
Hi, thanks for clarifying. Unfortunately there's no such feature in PyCharm, so feel free to submit a feature request to youtrack.jetbrains.com/issues
Check out Li Qimai's answer above ^^ :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.