命名张量运算符覆盖¶
请首先阅读 命名张量 以了解命名张量的介绍。
本文档是关于名称推断的参考,名称推断是一个定义命名张量的过程
使用名称来提供额外的自动运行时正确性检查
将名称从输入张量传播到输出张量
下面是所有命名张量支持的操作及其相关名称推断规则的列表。
如果您在此处没有看到您需要的操作,但它对您的用例有帮助,请搜索是否已提交问题,如果未提交,请提交一个问题。
警告
命名张量 API 是实验性的,可能会发生变化。
API |
名称推断规则 |
---|---|
请参阅文档 |
|
请参阅文档 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
|
无 |
|
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
请参阅文档 |
|
无 |
|
无 |
|
|
无 |
无 |
|
|
请参阅文档 |
|
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
|
无 |
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
|
|
无 |
|
将掩码与输入对齐,然后统一来自输入张量的名称 |
|
请参阅文档 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
|
无 |
无 |
|
无 |
|
请参阅文档 |
|
无 |
|
无 |
|
请参阅文档 |
|
请参阅文档 |
|
无 |
|
无 |
|
仅允许不改变形状的调整大小 |
|
仅允许不改变形状的调整大小 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
请参阅文档 |
|
无 |
|
无 |
|
保留输入名称¶
所有逐点一元函数以及其他一些一元函数都遵循此规则。
检查名称:无
传播名称:输入张量的名称会传播到输出。
>>> x = torch.randn(3, 3, names=('N', 'C'))
>>> x.abs().names
('N', 'C')
移除维度¶
所有归约操作(如 sum()
)通过在所需的维度上进行归约来移除维度。其他操作(如 select()
和 squeeze()
)也会移除维度。
在任何可以向运算符传递整数维度索引的地方,也可以传递维度名称。接受维度索引列表的函数也可以接受维度名称列表。
检查名称:如果
dim
或dims
作为名称列表传入,请检查这些名称是否存在于self
中。传播名称:如果
dim
或dims
指定的输入张量的维度在输出张量中不存在,则这些维度的对应名称不会出现在output.names
中。
>>> x = torch.randn(1, 3, 3, 3, names=('N', 'C', 'H', 'W'))
>>> x.squeeze('N').names
('C', 'H', 'W')
>>> x = torch.randn(3, 3, 3, 3, names=('N', 'C', 'H', 'W'))
>>> x.sum(['N', 'C']).names
('H', 'W')
# Reduction ops with keepdim=True don't actually remove dimensions.
>>> x = torch.randn(3, 3, 3, 3, names=('N', 'C', 'H', 'W'))
>>> x.sum(['N', 'C'], keepdim=True).names
('N', 'C', 'H', 'W')
统一来自输入的名称¶
所有二元算术运算都遵循此规则。广播运算仍然从右侧按位置广播,以保持与未命名张量的兼容性。要按名称执行显式广播,请使用 Tensor.align_as()
。
检查名称:所有名称必须从右侧按位置匹配。即,在
tensor + other
中,对于(-min(tensor.dim(), other.dim()) + 1, -1]
中的所有i
,match(tensor.names[i], other.names[i])
必须为真。检查名称:此外,所有已命名的维度必须从右侧对齐。在匹配期间,如果我们将已命名的维度
A
与未命名的维度None
匹配,则A
不得出现在具有未命名维度的张量中。传播名称:从右侧统一来自两个张量的名称对,以生成输出名称。
例如,
# tensor: Tensor[ N, None]
# other: Tensor[None, C]
>>> tensor = torch.randn(3, 3, names=('N', None))
>>> other = torch.randn(3, 3, names=(None, 'C'))
>>> (tensor + other).names
('N', 'C')
检查名称
match(tensor.names[-1], other.names[-1])
为True
match(tensor.names[-2], tensor.names[-2])
为True
因为我们在
tensor
中将None
与'C'
匹配,请检查以确保'C'
不存在于tensor
中(它不存在)。检查以确保
'N'
不存在于other
中(它不存在)。
最后,输出名称通过 [unify('N', None), unify(None, 'C')] = ['N', 'C']
计算得出
更多示例
# Dimensions don't match from the right:
# tensor: Tensor[N, C]
# other: Tensor[ N]
>>> tensor = torch.randn(3, 3, names=('N', 'C'))
>>> other = torch.randn(3, names=('N',))
>>> (tensor + other).names
RuntimeError: Error when attempting to broadcast dims ['N', 'C'] and dims
['N']: dim 'C' and dim 'N' are at the same position from the right but do
not match.
# Dimensions aren't aligned when matching tensor.names[-1] and other.names[-1]:
# tensor: Tensor[N, None]
# other: Tensor[ N]
>>> tensor = torch.randn(3, 3, names=('N', None))
>>> other = torch.randn(3, names=('N',))
>>> (tensor + other).names
RuntimeError: Misaligned dims when attempting to broadcast dims ['N'] and
dims ['N', None]: dim 'N' appears in a different position from the right
across both lists.
注意
在最后两个示例中,都可以按名称对齐张量,然后执行加法。使用 Tensor.align_as()
按名称对齐张量,或使用 Tensor.align_to()
将张量对齐到自定义维度顺序。
置换维度¶
某些操作(如 Tensor.t()
)会置换维度的顺序。维度名称附加到各个维度,因此它们也会被置换。
如果运算符接受位置索引 dim
,它也能够接受维度名称作为 dim
。
检查名称:如果
dim
作为名称传递,请检查它是否存在于张量中。传播名称:以与要置换的维度相同的方式置换维度名称。
>>> x = torch.randn(3, 3, names=('N', 'C'))
>>> x.transpose('N', 'C').names
('C', 'N')
收缩维度¶
矩阵乘法函数遵循此规则的某些变体。让我们首先了解一下 torch.mm()
,然后概括批量矩阵乘法的规则。
对于 torch.mm(tensor, other)
检查名称:无
传播名称:结果名称为
(tensor.names[-2], other.names[-1])
。
>>> x = torch.randn(3, 3, names=('N', 'D'))
>>> y = torch.randn(3, 3, names=('in', 'out'))
>>> x.mm(y).names
('N', 'out')
本质上,矩阵乘法对两个维度执行点积,从而折叠它们。当两个张量进行矩阵乘法时,收缩的维度会消失,并且不会出现在输出张量中。
torch.mv()
,torch.dot()
的工作方式类似:名称推断不检查输入名称,并移除点积中涉及的维度
>>> x = torch.randn(3, 3, names=('N', 'D'))
>>> y = torch.randn(3, names=('something',))
>>> x.mv(y).names
('N',)
现在,让我们看一下 torch.matmul(tensor, other)
。假设 tensor.dim() >= 2
且 other.dim() >= 2
。
检查名称:检查输入的批量维度是否对齐且可广播。有关输入对齐含义的信息,请参阅 统一来自输入的名称。
传播名称:结果名称通过统一批量维度并移除收缩的维度来获得:
unify(tensor.names[:-2], other.names[:-2]) + (tensor.names[-2], other.names[-1])
。
示例
# Batch matrix multiply of matrices Tensor['C', 'D'] and Tensor['E', 'F'].
# 'A', 'B' are batch dimensions.
>>> x = torch.randn(3, 3, 3, 3, names=('A', 'B', 'C', 'D'))
>>> y = torch.randn(3, 3, 3, names=('B', 'E', 'F'))
>>> torch.matmul(x, y).names
('A', 'B', 'C', 'F')
最后,许多 matmul 函数都有融合的 add
版本。即 addmm()
和 addmv()
。这些被视为组合的名称推断,例如 mm()
和 add()
的名称推断。
工厂函数¶
工厂函数现在接受一个新的 names
参数,该参数将名称与每个维度关联起来。
>>> torch.zeros(2, 3, names=('N', 'C'))
tensor([[0., 0., 0.],
[0., 0., 0.]], names=('N', 'C'))
out 函数和原地变体¶
指定为 out=
张量的张量具有以下行为
如果它没有已命名的维度,则从操作计算出的名称将传播到它。
如果它有任何已命名的维度,则从操作计算出的名称必须与现有名称完全相等。否则,操作会出错。
所有原地方法都会修改输入,使其名称等于名称推断中计算出的名称。例如
>>> x = torch.randn(3, 3)
>>> y = torch.randn(3, 3, names=('N', 'C'))
>>> x.names
(None, None)
>>> x += y
>>> x.names
('N', 'C')