如何在 Pytorch 的 `nn.Sequential` 中展平输入 [英] how to flatten input in `nn.Sequential` in Pytorch

查看:22
本文介绍了如何在 Pytorch 的 `nn.Sequential` 中展平输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 nn.Sequential 中展平输入

how to flatten input inside the 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://Discussion.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` 中展平输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆