评价此页

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 (boolList[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.

警告

dim_order 张量 API 处于实验阶段,可能会发生更改。