what about
def template(num_layers):
    if num_layers == 1:
        return nn.Sequential([a,A])
    else:
        return nn.Sequential([f(num_layers), template(num_layers-1), F(num_layers)])

If I recall correctly lists of modules/tensors within a module is tricky : https://pytorch.slack.com/archives/general/p1485447780001394 15