本文介绍了如何在Pytorch的`nn.Sequential`中变平输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何展平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))
推荐答案
您可以如下创建一个新的模块/类,并像使用其他模块一样依次使用它(调用Flatten()
).
You can create a new module/class as below and use it in the sequential as you are using other modules (call Flatten()
).
class Flatten(torch.nn.Module):
def forward(self, x):
batch_size = x.shape[0]
return x.view(batch_size, -1)
参考: https://ask.pytorch.org/t/flatten-layer-of-pytorch-build-by-sequential-container/5983
Flatten
现在是割炬的一部分.参见 https://pytorch.org/docs/stable/nn.html?highlight = flatten#torch.nn.Flatten
Flatten
is part of torch now. See https://pytorch.org/docs/stable/nn.html?highlight=flatten#torch.nn.Flatten
这篇关于如何在Pytorch的`nn.Sequential`中变平输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!