为什么在此示例中此tensor.unfold方法添加尺寸?

问题描述 投票:0回答:1

我已经熟悉https://pytorch.org/docs/stable/tensors.html#torch.Tensor.unfold中的Pytorch展开方法>

我看了他们的例子是>

>>> x = torch.arange(1., 8)
>>> x
tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.])
>>> x.unfold(0, 2, 1)
tensor([[ 1.,  2.],
        [ 2.,  3.],
        [ 3.,  4.],
        [ 4.,  5.],
        [ 5.,  6.],
        [ 6.,  7.]])

我上面了解到,当我们在维度0上展开时,我们一次跨度为2时会获取大小为1的块,因此,结果是排列了不同的块,即[1., 2.],[ C0]等。由于最后有[2., 3.]个块,这些块将放在一起,最终形状为6

但是,我还有另一个示例,如下所示。

(6,2)

所以您看到我的原始张量是In [115]: s = torch.arange(20).view(1,10,2) In [116]: s Out[116]: tensor([[[ 0, 1], [ 2, 3], [ 4, 5], [ 6, 7], [ 8, 9], [10, 11], [12, 13], [14, 15], [16, 17], [18, 19]]]) In [117]: s.unfold(0,1,1) Out[117]: tensor([[[[ 0], [ 1]], [[ 2], [ 3]], [[ 4], [ 5]], [[ 6], [ 7]], [[ 8], [ 9]], [[10], [11]], [[12], [13]], [[14], [15]], [[16], [17]], [[18], [19]]]]) In [119]: s.unfold(0,1,1).shape Out[119]: torch.Size([1, 10, 2, 1]) 形状,我要求使用参数(1,10,2)进行展开操作。

[根据上一个示例的原始理解,我认为这意味着在维度s.unfold(0, 1, 1)中,我们一次跨度为01块。因此,当我们进入维度1时,我们看到我们只有一个大小0的块。因此,输出应该刚好占用了该块,并且可能应该增加了一个包装该块的尺寸,并给了我一个大小为(10,2)的输出。

但是,它的输出为(1, 10, 2)。为什么最后有一个额外的维度?有人可以直观地详细说明吗?

我熟悉https://pytorch.org/docs/stable/tensors.html#torch.Tensor.unfold中的Pytorch展开方法,我看了他们的示例>>> x = torch.arange(1 。,8)...

pytorch tensor
1个回答
0
投票

文档说明:

在返回的张量中附加了尺寸为(1, 10, 2, 1)的附加尺寸。

© www.soinside.com 2019 - 2024. All rights reserved.