Skip to main content

Patch torch.Tensor.__repr__ to include tensor shape.

Project description

array_repr_shape

A tiny utility that patches torch.Tensor.__repr__ and numpy.ndarray.__repr__ so that every printed tensor/ndarray shows its shape.

一个很小的工具,用来给 torch.Tensor.__repr__numpy.ndarray.__repr__ 打补丁,让你在 print(tensor) / print(array) 时一眼看到张量 / 数组的形状,在 VS Code 的 Debugger、REPL、Jupyter 中都更方便调试。

示例输出(PyTorch):

{Tensor:(2, 3)} tensor([[...]])

示例输出(NumPy):

{ndarray:(2, 3)} array([[...]])

也就是说,原本的输出前面会多一段 {Tensor:(shape)}{ndarray:(shape)}


Features / 特性

  • Torch + NumPy 支持:同时支持 torch.Tensornumpy.ndarray__repr__ 形状前缀。
  • Lazy loading / 惰性加载:可以在导入 torch / numpy 之前或之后调用,首次导入对应模块时自动打补丁。
  • Idempotent / 幂等:多次调用 enable_torch_shape() / enable_numpy_shape() 不会重复打补丁。
  • Per-module disable / 独立关闭:通过环境变量分别控制 Torch 和 NumPy 的补丁。
  • Fail-safe / 安全兜底:如果补丁失败,会自动忽略错误,不影响你的正常代码。

Installation / 安装

pip install array_repr_shape

请确保你已经在环境中安装了 PyTorch 与 / 或 NumPy(如 torch, torchvision, numpy 等),本包只对已有的库做输出增强。


Quick Start / 快速上手

PyTorch

在任何需要的地方调用一次 enable_torch_shape() 即可:

from array_repr_shape import enable_torch_shape

enable_torch_shape()

import torch

x = torch.randn(2, 3)
print(x)

典型输出类似:

{Tensor:(2, 3)} tensor([[ 0.1234, -0.5678, ...],
                        [...,      ...,      ...]])

你也可以在已经导入 torch 之后调用,效果相同。

NumPy

开启 NumPy 的 shape 前缀:

from array_repr_shape import enable_numpy_shape

enable_numpy_shape()

import numpy as np

a = np.zeros((2, 3))
print(a)

典型输出类似:

{ndarray:(2, 3)} array([[0., 0., 0.],
                        [0., 0., 0.]])

Torch + NumPy 一起用

from array_repr_shape import enable_torch_shape, enable_numpy_shape

enable_torch_shape()
enable_numpy_shape()

import torch
import numpy as np

print(torch.ones(2, 2))
print(np.ones((2, 2)))

Disable via env / 环境变量关闭

如果某些场景下你不想改写 __repr__,可以通过环境变量分别一键关闭:

  • 关闭 Torch 补丁:

    DISABLE_TORCH_REPR_SHAPE=1 python your_script.py
    
  • 关闭 NumPy 补丁:

    DISABLE_NUMPY_REPR_SHAPE=1 python your_script.py
    

当环境变量为 1 时,对应的 enable_torch_shape() / enable_numpy_shape() 会直接返回,不做任何修改。


How it works / 实现原理

  • 通过一个轻量的 import hook 统一管理多个模块的懒加载补丁:
    • 如果模块(如 torchnumpy)已经在 sys.modules 中,立即打补丁。
    • 如果还未导入,则登记到 pending 列表中,首次导入时自动执行补丁。
    • 当所有待处理模块都已补丁完成后,恢复原始的 builtins.__import__,不长期影响 import 行为。
  • torch.Tensor.__repr__ 的新实现:在原有输出前加上 {Tensor:(shape)},形如 {Tensor:(2, 3, 4)}
  • numpy.ndarray.__repr__ 的新实现:在原有输出前加上 {ndarray:(shape)},形如 {ndarray:(2, 3, 4)}
  • 通过内部标记属性 __repr__adds_shape__ 确保不会重复打补丁。

Use cases / 适用场景

  • 调试深度学习模型时,快速区分多个 tensor / ndarray 的尺寸。
  • 在 VS Code Debugger、REPL、Jupyter Notebook 中自动显示 shape,减少 print(x.shape)
  • 在复杂数据管道中追踪中间结果的维度变化。

Development / 开发信息

欢迎在 PyPI 上使用 array_repr_shape 并在实际项目中反馈问题或改进建议。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

array_print_shape-0.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

array_print_shape-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file array_print_shape-0.0.1.tar.gz.

File metadata

  • Download URL: array_print_shape-0.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for array_print_shape-0.0.1.tar.gz
Algorithm Hash digest
SHA256 719aa2e9d37c48a88450c30c63cca57c44b9fd6e1f0b3aafdd431c05f66a7c29
MD5 a351725ac3ee6fea389085f67f974434
BLAKE2b-256 508dd118bcc083b21dc16473d2d51f737986f09a088afffbd908263d69135b0e

See more details on using hashes here.

File details

Details for the file array_print_shape-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for array_print_shape-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1d3fa3a1619ffdeb5a0c25f535980b8946153e8bc56633df55d8be50c32224a7
MD5 f8330180738513107d983870dd85b4df
BLAKE2b-256 b6a68d9ef134f97bff81003e5abfb0c27709ba6e6bfcba91171b3fc7f900cfbb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page