1. torch.stack 和 torch.cat
torch.stack(tensors, dim=0, *, out=None) → Tensor
- Concatenates a sequence of tensors along a new dimension.
- All tensors need to be of the same size.
torch.cat(tensors, dim=0, *, out=None) → Tensor
- Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.
- torch.stack会新加增加一个维度,可以理解为叠加。
- torch.cat会增加现有维度的值,可以理解为续接。
>>> a = torch.Tensor([1, 2, 3])
>>> a.size()
torch.Size([3])
>>> torch.stack((a, a)).size()
torch.Size([2, 3])
>>> torch.cat((a, a)).size()
torch.Size([6])
2. torch.index_select
torch.index_select(input, dim, index, *, out=None) → Tensor
- Returns a new tensor which indexes the input tensor along dimension dim using the entries in index which is a LongTensor.
- The returned tensor has the same number of dimensions as the original tensor (input). The dimth dimension has the same size as the length of index; other dimensions have the same size as in the original tensor.
>>> a = torch.arange(1, 13).view(3, 4)
>>> a
tensor([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> b = torch.index_select(a, 0, torch.tensor([0, 2]))
>>> b
tensor([[ 1, 2, 3, 4],
[ 9, 10, 11, 12]])
>>> a.index_select(0, torch.tensor([0, 2])) # 另一种用法
tensor([[ 1, 2, 3, 4],
[ 9, 10, 11, 12]])
>>> c = torch.index_select(a, 1, torch.tensor([1, 3]))
>>> c
tensor([[ 2, 4],
[ 6, 8],
[10, 12]])
3. detach 和 detach_
detach()
- Returns a new Tensor, detached from the current graph.
- The result will never require gradient.
- Returned Tensor shares the same storage with the original one. In-place modifications on either of them will be seen, and may trigger errors in correctness checks.
detach_()
- Detaches the Tensor from the graph that created it, making it a leaf. Views cannot be detached in-place.
4. torch.unsqueeze
torch.unsqueeze(input, dim) → Tensor
- Returns a new tensor with a dimension of size one inserted at the specified position.
- The returned tensor shares the same underlying data with this tensor.
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1, 2, 3, 4]])
>>> x.unsqueeze(1) # 另一种用法
tensor([[1],
[2],
[3],
[4]])