如何在Pytorch的`nn.Sequential`中变平输入

Kha*_*dra 5 python artificial-intelligence neural-network pytorch

如何扁平化内部的输入 nn.Sequential

Model = nn.Sequential(x.view(x.shape[0],-1),
                     nn.Linear(784,256),
                     nn.ReLU(),
                     nn.Linear(256,128),
                     nn.ReLU(),
                     nn.Linear(128,64),
                     nn.ReLU(),
                     nn.Linear(64,10),
                     nn.LogSoftmax(dim=1))
Run Code Online (Sandbox Code Playgroud)

Uma*_*pta 9

您可以如下创建一个新的模块/类,并像使用其他模块一样依次使用它(调用Flatten())。

class Flatten(torch.nn.Module):
    def forward(self, x):
        batch_size = x.shape[0]
        return x.view(batch_size, -1)
Run Code Online (Sandbox Code Playgroud)

参考:https : //discuss.pytorch.org/t/flatten-layer-of-pytorch-build-by-sequential-container/5983

  • @DanielMöller再次看到问题,OP希望使用nn.Sequential` (2认同)

pro*_*sti 5

作为被定义的flatten方法

torch.flatten(input, start_dim=0, end_dim=-1) ? Tensor
Run Code Online (Sandbox Code Playgroud)

速度与 相当view(),但reshape速度更快。

import torch.nn as nn

class Flatten(nn.Module):
    def forward(self, input):
        return input.view(input.size(0), -1)

flatten = Flatten()

t = torch.Tensor(3,2,2).random_(0, 10)
print(t, t.shape)


#https://pytorch.org/docs/master/torch.html#torch.flatten
f = torch.flatten(t, start_dim=1, end_dim=-1)
print(f, f.shape)


#https://pytorch.org/docs/master/torch.html#torch.view
f = t.view(t.size(0), -1)
print(f, f.shape)


#https://pytorch.org/docs/master/torch.html#torch.reshape
f = t.reshape(t.size(0), -1)
print(f, f.shape)
Run Code Online (Sandbox Code Playgroud)

速度检查

# flatten 3.49 µs ± 146 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# view 3.23 µs ± 228 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# reshape 3.04 µs ± 93 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Run Code Online (Sandbox Code Playgroud)

如果我们使用上面的类

flatten = Flatten()
t = torch.Tensor(3,2,2).random_(0, 10)
%timeit f=flatten(t)


5.16 µs ± 122 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Run Code Online (Sandbox Code Playgroud)

这个结果表明创建一个类会是较慢的方法。这就是为什么将张量向前展平更快的原因。我认为这是他们没有晋升的主要原因nn.Flatten

所以我的建议是使用内线来提高速度。像这样的东西:

out = inp.reshape(inp.size(0), -1)
Run Code Online (Sandbox Code Playgroud)


plh*_*lhn 5

您可以按如下方式修改您的代码,

Model = nn.Sequential(nn.Flatten(0, -1),
                     nn.Linear(784,256),
                     nn.ReLU(),
                     nn.Linear(256,128),
                     nn.ReLU(),
                     nn.Linear(128,64),
                     nn.ReLU(),
                     nn.Linear(64,10),
                     nn.LogSoftmax(dim=1))
Run Code Online (Sandbox Code Playgroud)

  • 我认为它应该是“Flatten(1, -1)”,因为你想保持批量大小。 (2认同)