評價此頁

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.

警告

Tensor.dim_order API 是實驗性的,可能會發生更改。