flatten.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # mypy: allow-untyped-defs
  2. from typing import Union
  3. from torch import Tensor
  4. from torch.types import _size
  5. from .module import Module
  6. __all__ = ["Flatten", "Unflatten"]
  7. class Flatten(Module):
  8. r"""
  9. Flattens a contiguous range of dims into a tensor.
  10. For use with :class:`~nn.Sequential`, see :meth:`torch.flatten` for details.
  11. Shape:
  12. - Input: :math:`(*, S_{\text{start}},..., S_{i}, ..., S_{\text{end}}, *)`,'
  13. where :math:`S_{i}` is the size at dimension :math:`i` and :math:`*` means any
  14. number of dimensions including none.
  15. - Output: :math:`(*, \prod_{i=\text{start}}^{\text{end}} S_{i}, *)`.
  16. Args:
  17. start_dim: first dim to flatten (default = 1).
  18. end_dim: last dim to flatten (default = -1).
  19. Examples::
  20. >>> input = torch.randn(32, 1, 5, 5)
  21. >>> # With default parameters
  22. >>> m = nn.Flatten()
  23. >>> output = m(input)
  24. >>> output.size()
  25. torch.Size([32, 25])
  26. >>> # With non-default parameters
  27. >>> m = nn.Flatten(0, 2)
  28. >>> output = m(input)
  29. >>> output.size()
  30. torch.Size([160, 5])
  31. """
  32. __constants__ = ["start_dim", "end_dim"]
  33. start_dim: int
  34. end_dim: int
  35. def __init__(self, start_dim: int = 1, end_dim: int = -1) -> None:
  36. super().__init__()
  37. self.start_dim = start_dim
  38. self.end_dim = end_dim
  39. def forward(self, input: Tensor) -> Tensor:
  40. """
  41. Runs the forward pass.
  42. """
  43. return input.flatten(self.start_dim, self.end_dim)
  44. def extra_repr(self) -> str:
  45. """
  46. Returns the extra representation of the module.
  47. """
  48. return f"start_dim={self.start_dim}, end_dim={self.end_dim}"
  49. class Unflatten(Module):
  50. r"""
  51. Unflattens a tensor dim expanding it to a desired shape. For use with :class:`~nn.Sequential`.
  52. * :attr:`dim` specifies the dimension of the input tensor to be unflattened, and it can
  53. be either `int` or `str` when `Tensor` or `NamedTensor` is used, respectively.
  54. * :attr:`unflattened_size` is the new shape of the unflattened dimension of the tensor and it can be
  55. a `tuple` of ints or a `list` of ints or `torch.Size` for `Tensor` input; a `NamedShape`
  56. (tuple of `(name, size)` tuples) for `NamedTensor` input.
  57. Shape:
  58. - Input: :math:`(*, S_{\text{dim}}, *)`, where :math:`S_{\text{dim}}` is the size at
  59. dimension :attr:`dim` and :math:`*` means any number of dimensions including none.
  60. - Output: :math:`(*, U_1, ..., U_n, *)`, where :math:`U` = :attr:`unflattened_size` and
  61. :math:`\prod_{i=1}^n U_i = S_{\text{dim}}`.
  62. Args:
  63. dim (Union[int, str]): Dimension to be unflattened
  64. unflattened_size (Union[torch.Size, Tuple, List, NamedShape]): New shape of the unflattened dimension
  65. Examples:
  66. >>> input = torch.randn(2, 50)
  67. >>> # With tuple of ints
  68. >>> m = nn.Sequential(
  69. >>> nn.Linear(50, 50),
  70. >>> nn.Unflatten(1, (2, 5, 5))
  71. >>> )
  72. >>> output = m(input)
  73. >>> output.size()
  74. torch.Size([2, 2, 5, 5])
  75. >>> # With torch.Size
  76. >>> m = nn.Sequential(
  77. >>> nn.Linear(50, 50),
  78. >>> nn.Unflatten(1, torch.Size([2, 5, 5]))
  79. >>> )
  80. >>> output = m(input)
  81. >>> output.size()
  82. torch.Size([2, 2, 5, 5])
  83. >>> # With namedshape (tuple of tuples)
  84. >>> input = torch.randn(2, 50, names=("N", "features"))
  85. >>> unflatten = nn.Unflatten("features", (("C", 2), ("H", 5), ("W", 5)))
  86. >>> output = unflatten(input)
  87. >>> output.size()
  88. torch.Size([2, 2, 5, 5])
  89. """
  90. NamedShape = tuple[tuple[str, int]]
  91. __constants__ = ["dim", "unflattened_size"]
  92. dim: Union[int, str]
  93. unflattened_size: Union[_size, NamedShape]
  94. def __init__(
  95. self, dim: Union[int, str], unflattened_size: Union[_size, NamedShape]
  96. ) -> None:
  97. super().__init__()
  98. if isinstance(dim, int):
  99. self._require_tuple_int(unflattened_size)
  100. elif isinstance(dim, str):
  101. self._require_tuple_tuple(unflattened_size)
  102. else:
  103. raise TypeError("invalid argument type for dim parameter")
  104. self.dim = dim
  105. self.unflattened_size = unflattened_size
  106. def _require_tuple_tuple(self, input) -> None:
  107. if isinstance(input, tuple):
  108. for idx, elem in enumerate(input):
  109. if not isinstance(elem, tuple):
  110. raise TypeError(
  111. "unflattened_size must be tuple of tuples, "
  112. + f"but found element of type {type(elem).__name__} at pos {idx}"
  113. )
  114. return
  115. raise TypeError(
  116. "unflattened_size must be a tuple of tuples, "
  117. + f"but found type {type(input).__name__}"
  118. )
  119. def _require_tuple_int(self, input) -> None:
  120. if isinstance(input, (tuple, list)):
  121. for idx, elem in enumerate(input):
  122. if not isinstance(elem, int):
  123. raise TypeError(
  124. "unflattened_size must be tuple of ints, "
  125. + f"but found element of type {type(elem).__name__} at pos {idx}"
  126. )
  127. return
  128. raise TypeError(
  129. f"unflattened_size must be a tuple of ints, but found type {type(input).__name__}"
  130. )
  131. def forward(self, input: Tensor) -> Tensor:
  132. """
  133. Runs the forward pass.
  134. """
  135. return input.unflatten(self.dim, self.unflattened_size)
  136. def extra_repr(self) -> str:
  137. """
  138. Returns the extra representation of the module.
  139. """
  140. return f"dim={self.dim}, unflattened_size={self.unflattened_size}"