"unsqueeze" 在 Pytorch 中做什么?
What does "unsqueeze" do in Pytorch?
Returns a new tensor with a dimension of size one inserted at the specified position. [...]
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1, 2, 3, 4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
[ 2],
[ 3],
[ 4]])
如果你查看数组前后的形状,你会发现它之前是 (4,)
,之后是 (1, 4)
(当第二个参数是 0
时)和(4, 1)
(当第二个参数为1
时)。因此根据第二个参数的值,1
被插入到轴 0
或 1
的数组形状中。
这与 np.squeeze()
(从 MATLAB 借用的术语)相反,它删除了大小为 1
(单例)的轴。
以下是 PyTorch 文档中的描述:
torch.squeeze(input, dim=None, *, out=None)
→ Tensor
Returns a tensor with all the dimensions of input
of size 1 removed.
For example, if input is of shape: (A×1×B×C×1×D) then the out tensor will be of shape: (A×B×C×D) .
When dim
is given, a squeeze operation is done only in the given dimension. If input is of shape: (A×1×B) , squeeze(input, 0)
leaves the tensor unchanged, but squeeze(input, 1)
will squeeze the tensor to the shape (A×B) .
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.
A dim
value within the range [-input.dim() - 1, input.dim() + 1)
can be used. Negative dim
will correspond to unsqueeze()
applied at dim = dim + input.dim() + 1
.
表示添加维度的位置。 torch.unsqueeze
为张量增加一个维度。
所以假设你有一个形状为 (3) 的张量,如果你在 0 位置添加一个维度,它将是形状 (1,3),这意味着 1 行和 3 列:
- 如果你有一个形状为 (2,2) 的二维张量,请在 0 位置添加一个额外的维度,这将导致张量的形状为 (1 ,2,2),表示1个通道,2行2列。如果你在第1个位置添加,它的形状是(2,1,2),所以它有2个通道,1行2列。
- 如果在1位置加上,就是(3,1),也就是3行1列。
- 如果在2位置添加,则张量的形状为(2,2,1),即2通道,2行1列。
unsqueeze
变成 n.d。张量为 (n+1).d。一个是通过添加一个深度为 1 的额外维度。但是,由于新维度应该跨越哪个轴是不明确的(即它应该在哪个方向上“未压缩”),这需要由 dim
参数指定.
例如unsqueeze
可以通过三种不同的方式应用于二维张量:
生成的未压缩张量具有相同的信息,但用于访问它们的索引不同。
unsqueeze是一种改变张量维度的方法,这样就可以进行张量乘法等操作。这基本上改变了维度以产生具有不同维度的张量。
例如:如果您想将大小为 (4) 的张量与大小为 (4, N, N) 的张量相乘,那么您将得到一个错误。
但是使用unsqueeze方法,可以将张量转换为大小(4,1,1)。现在因为它有一个大小为 1 的操作数,您将能够将两个张量相乘。
Returns a new tensor with a dimension of size one inserted at the specified position. [...]
>>> x = torch.tensor([1, 2, 3, 4]) >>> torch.unsqueeze(x, 0) tensor([[ 1, 2, 3, 4]]) >>> torch.unsqueeze(x, 1) tensor([[ 1], [ 2], [ 3], [ 4]])
如果你查看数组前后的形状,你会发现它之前是 (4,)
,之后是 (1, 4)
(当第二个参数是 0
时)和(4, 1)
(当第二个参数为1
时)。因此根据第二个参数的值,1
被插入到轴 0
或 1
的数组形状中。
这与 np.squeeze()
(从 MATLAB 借用的术语)相反,它删除了大小为 1
(单例)的轴。
以下是 PyTorch 文档中的描述:
torch.squeeze(input, dim=None, *, out=None)
→ TensorReturns a tensor with all the dimensions of
input
of size 1 removed.For example, if input is of shape: (A×1×B×C×1×D) then the out tensor will be of shape: (A×B×C×D) .
When
dim
is given, a squeeze operation is done only in the given dimension. If input is of shape: (A×1×B) ,squeeze(input, 0)
leaves the tensor unchanged, butsqueeze(input, 1)
will squeeze the tensor to the shape (A×B) .
torch.unsqueeze(input, dim)
→ TensorReturns 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.
A
dim
value within the range[-input.dim() - 1, input.dim() + 1)
can be used. Negativedim
will correspond tounsqueeze()
applied atdim = dim + input.dim() + 1
.
表示添加维度的位置。 torch.unsqueeze
为张量增加一个维度。
所以假设你有一个形状为 (3) 的张量,如果你在 0 位置添加一个维度,它将是形状 (1,3),这意味着 1 行和 3 列:
- 如果你有一个形状为 (2,2) 的二维张量,请在 0 位置添加一个额外的维度,这将导致张量的形状为 (1 ,2,2),表示1个通道,2行2列。如果你在第1个位置添加,它的形状是(2,1,2),所以它有2个通道,1行2列。
- 如果在1位置加上,就是(3,1),也就是3行1列。
- 如果在2位置添加,则张量的形状为(2,2,1),即2通道,2行1列。
unsqueeze
变成 n.d。张量为 (n+1).d。一个是通过添加一个深度为 1 的额外维度。但是,由于新维度应该跨越哪个轴是不明确的(即它应该在哪个方向上“未压缩”),这需要由 dim
参数指定.
例如unsqueeze
可以通过三种不同的方式应用于二维张量:
生成的未压缩张量具有相同的信息,但用于访问它们的索引不同。
unsqueeze是一种改变张量维度的方法,这样就可以进行张量乘法等操作。这基本上改变了维度以产生具有不同维度的张量。
例如:如果您想将大小为 (4) 的张量与大小为 (4, N, N) 的张量相乘,那么您将得到一个错误。 但是使用unsqueeze方法,可以将张量转换为大小(4,1,1)。现在因为它有一个大小为 1 的操作数,您将能够将两个张量相乘。