当我使用 PyCharm 进行调试时,我希望调试器显示我的 NumPy 数组/Jax 数组/PyTorch 张量的形状。相反,我看到了它们的值:
有没有办法配置 PyCharm 的调试器,使这些多维数组的形状显示出来?
最佳答案
我通过修改成员函数 torch.Tensor.__repr__
来实现这一点,因为 PyCharm 调试器调用 __repr__
来获取对象的字符串表示形式。
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
在 PyCharm 调试器中,您将看到如下表示:
>>>print(torch.ones(3,3))
Size([3, 3]) float32@cpu
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
关于intellij-idea - 配置 PyCharm 调试器以显示数组/张量形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63268967/