How to watch the values of the local variables in functions?

Dear friends,

I am using spyder to debug a project built on PyTorch, I am in the trouble of how can I watch the values of the local variables in the Variable Explorer panel, please? With that, I can see the process in detail.

Thank you in advance!

Tensor values are inspectable just like any other python variable. I usually just set breakpoint() in my code or use a library like. I haven’t checked whether Spyder renders things correctly but I believe it should, VS Code and Pycharm do it just fine

import rich
rich.inspect(tensor)

Oh, thank you so much for your help!

I tried the approach, and got the following variable value as follows in the console panel, is it correct?

Thank you again!

!next
╭─────────────────────────── <class ‘torch.Tensor’> ───────────────────────────╮
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ tensor([2, 2, 2, 2, 2, 1, 2, 0], device=‘cuda:0’) │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│ │
│ data = tensor([2, 2, 2, 2, 2, 1, 2, 0], device=‘cuda:0’) │
│ device = device(type=‘cuda’, index=0) │
│ dtype = torch.int64 │
│ grad = None │
│ grad_fn = None │
│ imag = RuntimeError(‘imag is not implemented for tensors with │
│ non-complex dtypes.’) │
│ is_cuda = True │
│ is_leaf = True │
│ is_meta = False │
│ is_mkldnn = False │
│ is_mlc = False │
│ is_ort = False │
│ is_quantized = False │
│ is_sparse = False │
│ is_sparse_csr = False │
│ is_vulkan = False │
│ is_xpu = False │
│ layout = torch.strided │
│ name = None │
│ names = (None,) │
│ ndim = 1 │
│ output_nr = 0 │
│ real = RuntimeError(‘real is not implemented for tensors with │
│ non-complex dtypes.’) │
│ requires_grad = False │
│ retains_grad = False │
│ shape = torch.Size([8]) │
│ T = tensor([2, 2, 2, 2, 2, 1, 2, 0], device=‘cuda:0’) │
╰──────────────────────────────────────────────────────────────────────────────╯

Yup that’s right a tensor is a data structure as you can see, if you just need the data then you can just pull it out with tensor.data