PyTorch torch.take_along_dim function
PyTorch torch reference manual
torch.take_along_dim is a function in PyTorch used to get elements at index positions along a specified dimension. It takes values based on the indices indices specified in, along the dim dimension, from input the input tensor.
Function definition
torch.take_along_dim(input, indices, dim)
Parameters:
input(Tensor): the input tensor.indices(Tensor): The index tensor specifying the positions of the elements to get. Its shape must be compatible with input along the dim dimension.dim(int): The dimension to take along.
Return Value:
torch.Tensor: Returns a new tensor composed of the elements taken by index.
Usage examples
Example
import
torch
Create a 2D tensor
x
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
,
[
7
,
8
,
9
]
]
)
(
"Original tensor:"
)
(
x
)
Take elements along dim=1
indices
=
torch.
tensor
(
[
[
0
,
1
,
2
]
,
[
2
,
1
,
0
]
,
[
0
,
0
,
0
]
]
)
y
=
torch.
take_along_dim
(
x
,
indices
,
dim
=
1
)
(
"nIndices:"
)
(
indices
)
(
"\nElements taken along dim=1:"
)
(
y
)
The output is:
原始张量:
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
索引:
tensor([[0, 1, 2],
[2, 1, 0],
[0, 0, 0]])
Elements taken along dim=1:
tensor([[1, 2, 3],
[6, 5, 4],
[7, 7, 7]])
Example
import
torch
Take elements along dim=0
x
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
,
[
7
,
8
,
9
]
]
)
Take different rows for each column
indices
=
torch.
tensor
(
[
[
0
,
1
,
2
]
,
[
2
,
0
,
1
]
,
[
1
,
2
,
0
]
]
)
y
=
torch.
take_along_dim
(
x
,
indices
,
dim
=
0
)
(
"Original tensor:"
)
(
x
)
(
"nIndices:"
)
(
indices
)
(
"\nElements taken along dim=0:"
)
(
y
)
The output is:
原始张量:
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
索引:
tensor([[0, 1, 2],
[2, 0, 1],
[1, 2, 0]])
Elements taken along dim=0:
tensor([[1, 5, 9],
[7, 2, 6],
[4, 8, 3]])
实例
import torch
# 在3D张量上使用
x = torch.arange(24).reshape(2, 3, 4)
print("原始形状:", x.shape)
# 沿 dim=1 取元素
indices = torch.tensor([[0, 1, 2],
[2, 0, 1]])
y = torch.take_along_dim(x, indices, dim=1)
print("索引形状:", indices.shape)
print("结果形状:", y.shape)
print("n结果:")
print(y)
输出结果为:
原始形状: torch.Size([2, 3, 4])
索引形状: torch.Size([2, 3])
结果形状: torch.Size([2, 3, 4])
结果:
tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[16, 17, 18, 19],
[12, 13, 14, 15],
[20, 21, 22, 23]]])
Example
import
torch
Application: Select specific elements along the batch dimension
For example, selecting a specific key-value in the attention mechanism
batch_size
=
2
num_heads
=
3
seq_len
=
4
head_dim
=
5
Simulate the attention weights of the query
attn_weights
=
torch.
randn
(
batch_size
,
num_heads
,
seq_len
)
Take the top-k indices for each head
k
=
2
indices
=
torch.
argsort
(
attn_weights
,
dim
=
1
,
descending
=
True
)
[
...
,
:k
]
(
"Index shape:"
,
indices.
shape
)
Simulate the value tensor
value
=
torch.
randn
(
batch_size
,
num_heads
,
seq_len
,
head_dim
)
Get the corresponding value along the seq_len dimension
selected_value
=
torch.
take_along_dim
(
value
,
indices.
unsqueeze
(
1
)
.
expand
(
1
,
1
,
1
,
head_dim
)
,
dim
=
2
)
(
"Value shape:"
,
value.
shape
)
(
"Selected Value shape:"
,
selected_value.
shape
)
The output is:
索引形状: torch.Size([2, 3, 4])
Value形状: torch.Size([2, 3, 4, 5])
选择的Value形状: torch.Size([2, 3, 2, 5])
注意:torch.take_along_dim 允许按维度进行索引,这比 torch.take 更加灵活,因为后者总是将张量视为一维的。
Pytorch torch 参考手册