关于torch.flatten的笔记

torch.nn.Flatten()可以是Sequential模型的一层,torch中定义的是:

def __init__(self, start_dim: int = 1, end_dim: int = -1) -> None:
    super(Flatten, self).__init__()
    self.start_dim = start_dim
    self.end_dim = end_dim

def forward(self, input: Tensor) -> Tensor:
    return input.flatten(self.start_dim, self.end_dim)

因此,它是从第一维(而不是第0维)推平到最后一维。因此,它保留了batchsize,仅把每个数据推平为一维向量。

维指的就是形状,如[2,3,4]

torch.flatten

先看函数参数:
torch.flatten(input, start_dim=0, end_dim=-1)
input: 一个 tensor,即要被“推平”的 tensor。

start_dim: “推平”的起始维度。

end_dim: “推平”的结束维度。

首先如果按照 start_dim 和 end_dim 的默认值,那么这个函数会把 input 推平成一个 shape 为 [n] 的tensor,其中 n 即 input 中元素个数。

如果我们要自己设定起始维度和结束维度呢?

我们要先来看一下 tensor 中的 shape 是怎么样的:

t = torch.tensor([[[1, 2, 2, 1],
                   [3, 4, 4, 3],
                   [1, 2, 3, 4]],
                  [[5, 6, 6, 5],
                   [7, 8, 8, 7],
                   [5, 6, 7, 8]]])
print(t, t.shape)

运行结果:

tensor([[[1, 2, 2, 1],
         [3, 4, 4, 3],
         [1, 2, 3, 4]],

        [[5, 6, 6, 5],
         [7, 8, 8, 7],
         [5, 6, 7, 8]]])
torch.Size([2, 3, 4])

我们可以看到,最外层的方括号内含两个元素,因此 shape 的第一个值是 2;类似地,第二层方括号里面含三个元素,shape 的第二个值就是 3;最内层方括号里含四个元素,shape 的第二个值就是 4。

直接使用flatten(默认):

>>> torch.flatten(t),torch.flatten(t).shape
(tensor([1, 2, 2, 1, 3, 4, 4, 3, 1, 2, 3, 4, 5, 6, 6, 5, 7, 8, 8, 7, 5, 6, 7, 8]), torch.Size([24]))

示例代码:

x = torch.flatten(t, start_dim=1)
print(x, x.shape)

y = torch.flatten(t, start_dim=0, end_dim=1)
print(y, y.shape)

运行结果:

tensor([[1, 2, 2, 1, 3, 4, 4, 3, 1, 2, 3, 4],
        [5, 6, 6, 5, 7, 8, 8, 7, 5, 6, 7, 8]]) torch.Size([2, 12])
tensor([[1, 2, 2, 1],
        [3, 4, 4, 3],
        [1, 2, 3, 4],
        [5, 6, 6, 5],
        [7, 8, 8, 7],
        [5, 6, 7, 8]]) torch.Size([6, 4])

可以看到,当 start_dim = 1 而 end_dim = −1 时,它把第 1 个维度到最后一个维度全部推平合并了。而当 start_dim = 0 而 end_dim = 1 时,它把第 0 个维度到第 1 个维度全部推平合并了。

(这里注意的一点是,维度是从第 0 维开始的)

而且,pytorch中的 torch.nn.Flatten 类和 torch.Tensor.flatten 方法其实都是基于上面的 torch.flatten 函数实现的。
转载自:https://dilthey.cnblogs.com/

posted @ 2021-08-04 21:25  zae  阅读(528)  评论(0编辑  收藏  举报