Flattens input by reshaping it into a one-dimensional tensor. If start_dim or end_dim are passed, only dimensions starting with start_dim and ending with end_dim are flattened. The order of elements in input is unchanged. Parameters: input : Tensor The input te
(input: Tensor, start_dim: int = 0, end_dim: int = -1)
| 1791 | |
| 1792 | |
| 1793 | def flatten(input: Tensor, start_dim: int = 0, end_dim: int = -1): |
| 1794 | ''' |
| 1795 | Flattens input by reshaping it into a one-dimensional tensor. |
| 1796 | |
| 1797 | If start_dim or end_dim are passed, only dimensions starting with start_dim and |
| 1798 | ending with end_dim are flattened. The order of elements in input is unchanged. |
| 1799 | |
| 1800 | Parameters: |
| 1801 | input : Tensor |
| 1802 | The input tensor to flatten. |
| 1803 | |
| 1804 | start_dim : int |
| 1805 | The first dim to flatten. |
| 1806 | |
| 1807 | end_dim : int |
| 1808 | The last dim to flatten. |
| 1809 | |
| 1810 | Returns: |
| 1811 | The tensor produced by the flatten layer. |
| 1812 | |
| 1813 | ''' |
| 1814 | shape = input.shape |
| 1815 | ndim = input.ndim() |
| 1816 | if start_dim < 0: start_dim += ndim |
| 1817 | if end_dim < 0: end_dim += ndim |
| 1818 | new_shape = list() |
| 1819 | for i in range(start_dim): |
| 1820 | new_shape.append(shape[i]) |
| 1821 | if end_dim - start_dim >= 0: |
| 1822 | flat_dim = 1 |
| 1823 | for i in range(start_dim, end_dim + 1): |
| 1824 | flat_dim *= shape[i] |
| 1825 | new_shape.append(flat_dim) |
| 1826 | for i in range(end_dim + 1, ndim): |
| 1827 | new_shape.append(shape[i]) |
| 1828 | return view(input, new_shape) |
| 1829 | |
| 1830 | |
| 1831 | def expand_dims(input: Tensor, |
no test coverage detected