快捷键

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 是实验性的,可能会更改。


© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源