torch.Tensor.dim_order ¬
- Tensor.dim_order(ambiguity_check=False) → tuple[source][source] ¬
返回唯一确定的描述维度顺序或物理布局的 int 元组。
维度顺序表示密集张量在内存中的布局方式,从最外层维度开始到最内层维度。
注意维度顺序可能并不总是唯一确定的。如果 ambiguity_check 为 True,当维度顺序无法唯一确定时,此函数将引发 RuntimeError;如果 ambiguity_check 是一个内存格式列表,当张量无法解释为给定的内存格式之一,或者无法唯一确定时,此函数将引发 RuntimeError。如果 ambiguity_check 为 False,它将返回一个合法的维度顺序(们),而不检查其唯一性。否则,将引发 TypeError。
- 参数:
ambiguity_check(布尔值或 torch.memory_format 列表)- 维度顺序模糊性的检查方法。
示例:
>>> torch.empty((2, 3, 5, 7)).dim_order() (0, 1, 2, 3) >>> torch.empty((2, 3, 5, 7)).transpose(1, 2).dim_order() (0, 2, 1, 3) >>> torch.empty((2, 3, 5, 7), memory_format=torch.channels_last).dim_order() (0, 2, 3, 1) >>> torch.empty((1, 2, 3, 4)).dim_order() (0, 1, 2, 3) >>> try: ... torch.empty((1, 2, 3, 4)).dim_order(ambiguity_check=True) ... except RuntimeError as e: ... print(e) The tensor does not have unique dim order, or cannot map to exact one of the given memory formats. >>> torch.empty((1, 2, 3, 4)).dim_order( ... ambiguity_check=[torch.contiguous_format, torch.channels_last] ... ) # It can be mapped to contiguous format (0, 1, 2, 3) >>> try: ... torch.empty((1, 2, 3, 4)).dim_order(ambiguity_check="ILLEGAL") ... except TypeError as e: ... print(e) The ambiguity_check argument must be a bool or a list of memory formats.
警告
torch.Tensor.dim_order API 是实验性的,可能会更改。