fmassa:

But if you definitely want to flatten your result inside a Sequential, you could define a module such as


Thanks, but this do not wok. it give me error messages
RuntimeError: size mismatch, m1: [4 x 784], m2: [144 x 120] at /b/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:1237
If I change Flatten to
class Flatten(nn.Module):
def forward(self, x):
x = x.view(-1, 1633)
return x
It give me error messages
RuntimeError: size ‘[-1 x 144]’ is invalid for input of with 3136 elements at /b/wheel/pytorch-src/torch/lib/TH/THStorage.c:55
gist of codes
class Flatten(nn.Module):
    def forward(self, x):
        x = x.view(x.size()[0], -1)
        return x

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        
        main = nn.Sequential()
        
        self._conv_block(main, 'conv_0', 3, 6, 5)
        main.add_module('max_pool_0_2_2', nn.MaxPool2d(2,2))
        
        self._conv_block(main, 'conv_1', 6, 16, 3)        
        main.add_module('max_pool_1_2_2', nn.MaxPool2d(2,2))
        
        main.add_module('flatten', Flatten())
        self._linear_block(main, 'linear_0', 16*3*3, 120)        
        self._linear_block(main, 'linear_1', 120, 84)
        main.add_module('linear_2-84-10.linear', nn.Linear(84, 10))
        
        self._main = main
        
    def forward(self, x):
        for module in self._modules.values():
            x = module(x)
        return x
        
    def _conv_block(self, main, name, inp_filter_size, out_filter_size, kernal_size):        
        main.add_module('{}-{}.{}.conv'.format(name, inp_filter_size, out_filter_size), 
                        nn.Conv2d(inp_filter_size, out_filter_size, kernal_size, 1, 1))
        main.add_module('{}-{}.batchnorm'.format(name, out_filter_size), nn.BatchNorm2d(out_filter_size))
        main.add_module('{}-{}.relu'.format(name, out_filter_size), nn.ReLU())                
        
    def _linear_block(self, main, name, inp_filter_size, out_filter_size):
        main.add_module('{}-{}.{}.linear'.format(name, inp_filter_size, out_filter_size), 
                        nn.Linear(inp_filter_size, out_filter_size))
        main.add_module('{}-{}'.format(name, out_filter_size), nn.ReLU())

Complete codes are place at pastebin 18, 90 lines



 fmassa:

In this case we would prefer to write the module with a class, and let nn.Sequential only for very simple functions.


I guess I would follow this advices, I pick sequential container because I do not know how to group several layer together(conv->batch->relu, define a residual block etc), until I find this example 163.