欢迎!对于新项目,我现在强烈建议您改用我的新型Jaxtyping项目。它支持pytorch,实际上并不依赖于jax,并且与火炬不同,它与静态类型的棋子兼容。 :)
转到这个:
def batch_outer_product ( x : torch . Tensor , y : torch . Tensor ) -> torch . Tensor :
# x has shape (batch, x_channels)
# y has shape (batch, y_channels)
# return has shape (batch, x_channels, y_channels)
return x . unsqueeze ( - 1 ) * y . unsqueeze ( - 2 )介入:
def batch_outer_product ( x : TensorType [ "batch" , "x_channels" ],
y : TensorType [ "batch" , "y_channels" ]
) -> TensorType [ "batch" , "x_channels" , "y_channels" ]:
return x . unsqueeze ( - 1 ) * y . unsqueeze ( - 2 )通过编程检查是否满足形状(dtype,...)规范。
再见虫子!向执行,清晰的代码文档打招呼。
如果(像我一样)您发现自己会用# x has shape (batch, hidden_state)或诸如assert x.shape == y.shape之类的语句,只是为了跟踪所有形状的内容,那么这是给您的。
pip install torchtyping需要Python> = 3.7和Pytorch> = 1.7.0。
如果使用typeguard ,则必须是版本<3.0.0。
torchtyping允许注释类型:
...的任意数量的批次尺寸;torchtyping是高度扩展的。如果(选项)安装了typeguard ,则可以在运行时检查类型,以确保张量确实具有广告的形状,DTYPE等。
# EXAMPLE
from torch import rand
from torchtyping import TensorType , patch_typeguard
from typeguard import typechecked
patch_typeguard () # use before @typechecked
@ typechecked
def func ( x : TensorType [ "batch" ],
y : TensorType [ "batch" ]) -> TensorType [ "batch" ]:
return x + y
func ( rand ( 3 ), rand ( 3 )) # works
func ( rand ( 3 ), rand ( 1 ))
# TypeError: Dimension 'batch' of inconsistent size. Got both 1 and 3. typeguard还具有一个导入挂钩,可用于自动测试整个模块,而无需手动添加@typeguard.typechecked Decorter。
如果您不使用typeguard ,则可以完全省略torchtyping.patch_typeguard() ,而torchtyping只用于文档目的。如果您尚未使用typeguard进行常规Python编程,请强烈考虑使用它。这是挤压错误的好方法。 typeguard和torchtyping都与pytest集成在一起,因此,如果您担心任何性能罚款,则只能在测试中启用它们。
torchtyping . TensorType [ shape , dtype , layout , details ]图书馆的核心。
每个shape , dtype , layout , details都是可选的。
shape论点可以是任何:int :尺寸必须完全如此。如果是-1 ,则允许任何尺寸。str :运行时通过的维度的大小将被绑定到此名称,所有张量检查尺寸是否一致。... :任何大小的任意数量的尺寸。str: int对(从技术上讲,这是一个切片),将str和int行为结合在一起。 (只是一个单独的str等同于str: -1 。)str: str对,在这种情况下,运行时传递的维度的大小将绑定到两个名称,并且所有名称的所有维度都必须具有相同的大小。 (有些人喜欢将其用作将多个名称与维度相关联的一种方式,出于额外的文档目的。)str: ...对,在这种情况下,与...相对应的多个维度将绑定到str指定的名称,并再次检查参数之间的一致性。None ,当与下面的is_named结合使用时,它表示一个尺寸,该维度一定不具有命名张量的名称。None: int对,既结合None and int行为。 (只是None本身就是等同None: -1 。)None: str对,既结合None and str行为。 (也就是说,它一定不能具有命名的维度,但必须与字符串的其他用途相一致。)typing.Any :任何尺寸都允许使用此维度(相当于-1 )。TensorType["batch": ..., "length": 10, "channels", -1] 。如果您只想指定尺寸的数量,则使用三维张量的TensorType[-1, -1, -1] 。dtype论点可以是:torch.float32 , torch.float64等int , bool , float ,它们转换为相应的pytorch类型。 float被专门解释为torch.get_default_dtype() ,通常为float32 。layout参数可以分别torch.sparse_coo torch.strided ,分别用于密集和稀疏的张量。details参数提供了一种传递任意数量的其他标志来自定义和扩展torchtyping方法。默认情况下,两个标志是内置的。 torchtyping.is_named会导致检查张量尺寸的名称,并可以使用torchtyping.is_float来检查是否传递了任意的torchtyping details 。(而不是仅仅是特定的浮点类型,例如TensorType[torch.float32] 。[]中,立即检查多件事。例如TensorType["batch": ..., "length", "channels", float, is_named] 。 torchtyping . patch_typeguard () torchtyping与typeguard集成以执行运行时类型检查。 torchtyping.patch_typeguard()应在全球级别上调用,并将修补typeguard检查TensorType s。
此功能可以安全多次运行。 (在第一次运行后它无能为力)。
@typeguard.typechecked ,则在使用@typeguard.typechecked之前,应随时调用torchtyping.patch_typeguard() 。例如,您可以使用torchtyping在每个文件的开头调用它。typeguard.importhook.install_import_hook ,则在定义要检查的功能之前,应随时调用torchtyping.patch_typeguard() 。例如,您只需与typeguard Import Hook同时调用一次torchtyping.patch_typeguard() 。 (钩子和补丁的顺序无关紧要。)typeguard ,则可以完全省略torchtyping.patch_typeguard() ,而torchtyping只用于文档目的。 pytest --torchtyping-patch-typeguard torchtyping提供了一个pytest插件,可以在测试之前自动运行torchtyping.patch_typeguard() 。 pytest会自动发现插件,您只需要传递--torchtyping-patch-typeguard标志即可启用它。然后可以使用@typeguard.typechecked , typeguard的导入挂钩或pytest flag --typeguard-packages="your_package_here" ,可以将软件包传递给typeguard 。
请参阅更多文档以下文档:
flake8和mypy兼容性;torchtyping编写自定义扩展;