torch.inner¶
- torch.inner(input, other, *, out=None) Tensor ¶
計算 1D tensor 的點積。對於更高維度,沿著最後一個維度對來自
input
和other
的元素乘積求和。注意
如果
input
或other
其中之一是純量,則結果等同於 torch.mul(input, other)。如果
input
和other
都不是純量,它們最後一個維度的大小必須匹配,且結果等同於 torch.tensordot(input, other, dims=([-1], [-1]))- 參數
- 關鍵字參數
out (Tensor, optional) – 可選的輸出張量,用於寫入結果。輸出形狀為 input.shape[:-1] + other.shape[:-1]。
範例
# Dot product >>> torch.inner(torch.tensor([1, 2, 3]), torch.tensor([0, 2, 1])) tensor(7) # Multidimensional input tensors >>> a = torch.randn(2, 3) >>> a tensor([[0.8173, 1.0874, 1.1784], [0.3279, 0.1234, 2.7894]]) >>> b = torch.randn(2, 4, 3) >>> b tensor([[[-0.4682, -0.7159, 0.1506], [ 0.4034, -0.3657, 1.0387], [ 0.9892, -0.6684, 0.1774], [ 0.9482, 1.3261, 0.3917]], [[ 0.4537, 0.7493, 1.1724], [ 0.2291, 0.5749, -0.2267], [-0.7920, 0.3607, -0.3701], [ 1.3666, -0.5850, -1.7242]]]) >>> torch.inner(a, b) tensor([[[-0.9837, 1.1560, 0.2907, 2.6785], [ 2.5671, 0.5452, -0.6912, -1.5509]], [[ 0.1782, 2.9843, 0.7366, 1.5672], [ 3.5115, -0.4864, -1.2476, -4.4337]]]) # Scalar input >>> torch.inner(a, torch.tensor(2)) tensor([[1.6347, 2.1748, 2.3567], [0.6558, 0.2469, 5.5787]])