Skip to content

Allow nn.Unflatten to take a Sequence[int] instead of specifically a Tuple[int, ...] #49743

@alanhdu

Description

@alanhdu

🚀 Feature

nn.Unflatten 's unflattened_size requires a tuple -- trying to create on using a list fails with:

>>> nn.Unflatten(1, [5, 5])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-b881e5cda02d> in <module>
----> 1 nn.Unflatten(1, [5, 5])

/opt/anaconda/envs/ctrldev/lib/python3.6/site-packages/torch/nn/modules/flatten.py in __init__(self, dim, unflattened_size)
    102 
    103         if isinstance(dim, int):
--> 104             self._require_tuple_int(unflattened_size)
    105         elif isinstance(dim, str):
    106             self._require_tuple_tuple(unflattened_size)

/opt/anaconda/envs/ctrldev/lib/python3.6/site-packages/torch/nn/modules/flatten.py in _require_tuple_int(self, input)
    128                                     "but found element of type {} at pos {}".format(type(elem).__name__, idx))
    129             return
--> 130         raise TypeError("unflattened_size must be a tuple of ints, but found type {}".format(type(input).__name__))
    131 
    132     def forward(self, input: Tensor) -> Tensor:

TypeError: unflattened_size must be a tuple of ints, but found type list

I think this is a little too restrictive -- is there any reason we couldn't accept an arbitrary Sequence instead of specificlaly a tuple? If necessary, it should be easy to convert them to tuples in the constructor.

cc @albanD @mruberry @jbschlosser

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNot as big of a feature, but technically not a bug. Should be easy to fixmodule: nnRelated to torch.nntriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions