-
torch.reshape()
将input
变换成指定形状的output
。🌞参数有两个: input
和 shape
"""
Parameters
input (Tensor) – the tensor to be reshaped
shape (tuple of int) – the new shape
"""
>>> import torch
>>> a = torch.arange(4)
>>> a
tensor([0, 1, 2, 3])
>>> torch.reshape(a, (2, 2))
tensor([[0, 1],
[2, 3]])
-
torch.Tensor.view
将input
变换成指定形状的output
。不能保证output在内存中的连续性,需要使用contiguous()
>>> import torch
>>> a = torch.arange(6)
>>> a
tensor([0, 1, 2, 3, 4, 5])
>>> a.view(2, 3)
tensor([[0, 1, 2],
[3, 4, 5]])
-
torch.permute
将input
的维度进行置换。🌞参数有两个: input
和 dims
"""
Parameters
input (Tensor) – the input tensor.
dims (tuple of int) – The desired ordering of dimensions
"""
>>> a = torch.arange(8).reshape(4, 2)
>>> a
tensor([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
>>> torch.permute(a, (1, 0))
tensor([[0, 2, 4, 6],
[1, 3, 5, 7]])
-
torch.transpose
将input
的维度进行置换。🌞参数有三个: input
和 dim1
和dim2
"""
Parameters
input (Tensor) – the input tensor.
dim0 (int) – the first dimension to be transposed
dim1 (int) – the second dimension to be transposed
"""
>>> a = torch.arange(10).reshape(2, 5)
>>> a
tensor([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> torch.transpose(a, 0, 1)
tensor([[0, 5],
[1, 6],
[2, 7],
[3, 8],
[4, 9]])
-
torch.squeeze
将input
中所有为1的维度删掉。🌞参数有两个: input
和 dim(可选)
"""
Parameters
input (Tensor) – the input tensor.
dim (int or tuple of ints, optional) –
"""
>>> import torch
>>> a = torch.arange(10).reshape(1,2, 5, 1)
>>> a.size()
torch.Size([1, 2, 5, 1])
>>> torch.squeeze(a).size()
torch.Size([2, 5])
-
torch.unsqueeze
将input
在指定位置扩展1个维度。🌞参数有两个: input
和 dim
"""
Parameters
input (Tensor) – the input tensor.
dim (int) – the index at which to insert the singleton dimension
"""
>>> import torch
>>> a = torch.arange(10).reshape(2, 5)
>>> a.size()
torch.Size([2, 5])
>>> torch.unsqueeze(a, 0).size()
torch.Size([1, 2, 5])
-
torch.cat
将多个input
按照指定维度拼接在一起。🌞参数有两个: 多个input
和 dim(可选)
,默认为0维
"""
Parameters
tensors (sequence of Tensors) – any python sequence of tensors of the same type. Non-empty tensors provided must have the same shape, except in the cat dimension.
dim (int, optional) – the dimension over which the tensors are concatenated
"""
>>> a = torch.randn((2, 3))
>>> b = torch.randn((2, 3))
>>> c = torch.cat((a, b))
>>> c.size()
torch.Size([4, 3])
>>> d = torch.cat((a, b), 1)
>>> d.size()
torch.Size([2, 6])
-
torch.split
可看作torch.cat
的反操作,将input
分为多块。🌞参数有三个: input
和 split_size_or_sections
和dim
"""
Parameters
tensor (Tensor) – tensor to split.
split_size_or_sections (int) or (list(int)) – size of a single chunk or list of sizes for each chunk
dim (int) – dimension along which to split the tensor.
"""
>>> a = torch.arange(10).reshape(5, 2)
>>> a
tensor([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> torch.split(a, 2)
(tensor([[0, 1],
[2, 3]]),
tensor([[4, 5],
[6, 7]]),
tensor([[8, 9]]))
>>> torch.split(a, [1, 4])
(tensor([[0, 1]]),
tensor([[2, 3],
[4, 5],
[6, 7],
[8, 9]]))
-
torch.chunk
可看作torch.cat
的反操作,将input
分为指定块数。🌞参数有三个: input
和 chunks
和dim
"""
Parameters
input (Tensor) – the tensor to split
chunks (int) – number of chunks to return
dim (int) – dimension along which to split the tensor
"""
>>> torch.arange(11).chunk(6)
(tensor([0, 1]),
tensor([2, 3]),
tensor([4, 5]),
tensor([6, 7]),
tensor([8, 9]),
tensor([10]))
>>> torch.arange(12).chunk(6)
(tensor([0, 1]),
tensor([2, 3]),
tensor([4, 5]),
tensor([6, 7]),
tensor([8, 9]),
tensor([10, 11]))
>>> torch.arange(13).chunk(6)
(tensor([0, 1, 2]),
tensor([3, 4, 5]),
tensor([6, 7, 8]),
tensor([ 9, 10, 11]),
tensor([12]))
-
torch.stack
沿着新的维度拼接多个input
。 🌞参数有两个: 多个input
和 dim(可选)
,默认在0维新增一个维度。
"""
Parameters
tensors (sequence of Tensors) – sequence of tensors to concatenate
dim (int, optional) – dimension to insert. Has to be between 0 and the number of dimensions of concatenated tensors (inclusive). Default: 0
"""
>>> a = torch.arange(9).reshape(3, 3)
>>> a
tensor([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> torch.stack((a, a), 0)
tensor([[[0, 1, 2],
[3, 4, 5],
[6, 7, 8]],
[[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]])
>>> torch.stack((a, a), 0).size()
torch.Size([2, 3, 3])