torch.Tensor.dim_order#
- Tensor.dim_order(ambiguity_check=False) tuple [source]#
返回一个唯一确定的整数元组,描述
self
的维度顺序或物理布局。维度顺序表示从最外层到最内层维度,维度在密集张量的内存中是如何布局的。
请注意,维度顺序可能并非总是唯一确定的。如果 ambiguity_check 为 True,当维度顺序无法唯一确定时,此函数将引发 RuntimeError;如果 ambiguity_check 是一个内存格式列表,当张量无法精确地解释为给定内存格式之一,或者无法唯一确定时,此函数将引发 RuntimeError。如果 ambiguity_check 为 False,它将返回一个合法的维度顺序,而不检查其唯一性。否则,它将引发 TypeError。
- 参数
ambiguity_check (bool 或 List[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.
警告
Tensor.dim_order API 是实验性的,可能会发生更改。