4

In PyCharm, the lengths of lists and shapes of numpy arrays and pytorch tensors are shown in the debugger as in here

, which enables us to get these attributes without having to expand the variable, scroll down, and find the attributes one by one. Is it possible to do this in VSCode?

f-sky
  • 41
  • 3

2 Answers2

1

If you are ok with monkey-patching (use with caution as explained here), you can dynamically replace the repr of any class. Add this to beginning of your debugged code, to get the Tensor repr you asked for. It is used by VSCode , but, may a

import torch
def custom_repr(self):
    return f'{{Tensor:{tuple(self.shape)}}} {original_repr(self)}'

original_repr = torch.Tensor.__repr__
torch.Tensor.__repr__ = custom_repr

You can do something similiar to the other classes you mentioned.

assaf lehr
  • 31
  • 3
0

In the watch view, you can write any expression. For example, instead of watching a list arr, you can watch len(arr). If you had a NumPy array, you could do arr.shape. Anything works.

If you don't know what the watch view is, you can find it by typing "watch" into the command palette: enter image description here

Nathan Dai
  • 392
  • 2
  • 11
  • 1
    Thanks a lot. However, this requires manually inputting the watch, what I want is to display them in the debugger automatically by maybe some configuration for the debugger. – f-sky Jan 14 '22 at 00:31