快捷方式

torch.Tensor.dim_order

Tensor.dim_order(ambiguity_check=False) tuple[源代码][源代码]

返回描述 self 的维度顺序或物理布局的唯一确定的 int 元组。

维度顺序表示密集 tensor 的维度如何在内存中布局,从最外层维度到最内层维度。

请注意,维度顺序可能并不总是唯一确定。如果 ambiguity_check 为 True,当维度顺序无法唯一确定时,此函数将引发 RuntimeError;如果 ambiguity_check 是内存格式的列表,当 tensor 无法被解释为正好是给定的内存格式之一,或者无法唯一确定时,此函数将引发 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 tensor API 处于实验阶段,可能会发生变化。

文档

访问 PyTorch 的完整开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并解答您的问题

查看资源