译(五十一)-Pytorch的unsqueeze有什么用
如有翻译问题欢迎评论指出,谢谢。
Pytorch的unsqueeze有什么用?
StarckOverflar asked:
- Pytorch 文档里的这个例子是什么意思?
- 返回一个在指定位置拓展一维的新张量。[...]
>>> 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]])
Answers:
norok2 - vote: 74
你可以观察一下数组处理前后的形状,第二个参数为
0
时,形状从(4,)
变成了(1, 4)
,第二个参数为1
时,则变成了(4, 1)
。即数组在第0
维和第1
维被拓展了一维,拓展的具体位置取决于第二个参数值。与它效果相反的是
np.squeeze()
(这种命名方式来自MATLAB),用来移除一个维度。iacob - vote: 34
unsqueeze
通过拓展一个额外的维度将 n 维张量转为 n+1 维。不过因为拓展位置的多样性,需要由dim
参数指定具体位置。例:
unsqueeze
能以三种方式应用到一个二维矩阵:这些输出的元素相同,但用于访问的索引不同。
Voontent - vote: 33
这是说在哪个地方拓展维度。
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 列的张量。
What does unsqueeze do in Pytorch?
StarckOverflar asked:
- I cannot understand how the example in the PyTorch documentation corresponds to the explanation:
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]])
- I cannot understand how the example in the PyTorch documentation corresponds to the explanation:
Answers:
norok2 - vote: 74
If you look at the shape of the array before and after, you see that before it was
(4,)
and after it is(1, 4)
(when second parameter is0
) and(4, 1)
(when second parameter is1
). So a1
was inserted in the shape of the array at axis0
or1
, depending on the value of the second parameter.
你可以观察一下数组处理前后的形状,第二个参数为0
时,形状从(4,)
变成了(1, 4)
,第二个参数为1
时,则变成了(4, 1)
。即数组在第0
维和第1
维被拓展了一维,拓展的具体位置取决于第二个参数值。That is opposite of
np.squeeze()
(nomenclature borrowed from MATLAB) which removes axes of size1
(singletons).
与它效果相反的是np.squeeze()
(这种命名方式来自MATLAB),用来移除一个维度。iacob - vote: 34
unsqueeze
turns an n.d. tensor into an (n+1).d. one by adding an extra dimension of depth 1. However, since it is ambiguous which axis the new dimension should lie across (i.e. in which direction it should be unsqueezed), this needs to be specified by thedim
argument.
unsqueeze
通过拓展一个额外的维度将 n 维张量转为 n+1 维。不过因为拓展位置的多样性,需要由dim
参数指定具体位置。e.g.
unsqueeze
can be applied to a 2d tensor three different ways:
例:unsqueeze
能以三种方式应用到一个二维矩阵:The resulting unsqueezed tensors have the same information, but the indices used to access them are different.
这些输出的元素相同,但用于访问的索引不同。Voontent - vote: 33
It indicates the position on where to add the dimension.
torch.unsqueeze
adds an additional dimension to the tensor.
这是说在哪个地方拓展维度。torch.unsqueeze
会拓展张量的维度。So let\'s say you have a tensor of shape (3), if you add a dimension at the 0 position, it will be of shape (1,3), which means 1 row and 3 columns:
例如有一个张量形状为 (3),如果在第 0 维拓展维度,则形状变为 (1, 3),即 1 行 3 列。- If you have a 2D tensor of shape (2,2) add add an extra dimension at the 0 position, this will result of the tensor having a shape of (1,2,2), which means one channel, 2 rows and 2 columns. If you add at the 1 position, it will be of shape (2,1,2), so it will have 2 channels, 1 row and 2 columns.
如果是形状为 (2, 2) 的二维矩阵在 0 维拓展,则形状变为 (1, 2, 2),即 1 通道 2 行 2 列的张量。如果添加在 1 维,则形状变为 (2, 1, 2),即 2 通道 1 行 2 列的张量。 - If you add at the 1 position, it will be (3,1), which means 3 rows and 1 column.
如果添加在 1 维,则形状变为 (3, 1),即 3 行 1 列。 - If you add it at the 2 position, the tensor will be of shape (2,2,1), which means 2 channels, 2 rows and one column.
如果添加在 2 维,则形状变为 (2, 2, 1),即 2 通道 2 行 1 列的张量。
- If you have a 2D tensor of shape (2,2) add add an extra dimension at the 0 position, this will result of the tensor having a shape of (1,2,2), which means one channel, 2 rows and 2 columns. If you add at the 1 position, it will be of shape (2,1,2), so it will have 2 channels, 1 row and 2 columns.
共有 0 条评论