intellij-idea - 配置 PyCharm 调试器以显示数组/张量形状?

标签 intellij-idea pycharm

当我使用 PyCharm 进行调试时,我希望调试器显示我的 NumPy 数组/Jax 数组/PyTorch 张量的形状。相反,我看到了它们的值:

enter image description here

有没有办法配置 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.]])

我的变量检查器的快照: PyCharm Debugger

关于intellij-idea - 配置 PyCharm 调试器以显示数组/张量形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63268967/

相关文章:

java - IntelliJ IDEA 可以显示像eclipse 这样的maven 项目吗?

git - 为什么我的 Intellij Git 插件在我单击提交后不提交文件?

java - 智能 : rerun intermittently failing random test with previos run's data

java - Kotlin:使用 lambda 代替函数式接口(interface)?

python - 动态更新 pydantic 中子类所有属性的类型提示

python - PhantomJS() 未在 pyCharm 中运行

java - 如何在 IntelliJ IDEA 中使用新的 Application Run 配置 UI 包含提供的依赖项?

python - 导入错误:C 扩展:没有名为 'parsing' 的模块未构建

python - 如何在 Pycharm pro *中以标准布局*查看 Jupyter notebook?

python - 如何在pycharm中将插入符号移动到控制台?