Add an operation to insert a singleton dimension to a tensor. That functions creates an operation that insert a singleton dimension (dimension of size 1) at position 'axis' in the output tensor. It works with negative values for the 'axis'. For example, for a tensor 'input' of
(input: Tensor, axis: int)
| 1928 | |
| 1929 | |
| 1930 | def unsqueeze(input: Tensor, axis: int): |
| 1931 | ''' |
| 1932 | Add an operation to insert a singleton dimension to a tensor. |
| 1933 | |
| 1934 | That functions creates an operation that insert a singleton dimension |
| 1935 | (dimension of size 1) at position 'axis' in the output tensor. It works with |
| 1936 | negative values for the 'axis'. |
| 1937 | |
| 1938 | For example, for a tensor 'input' of shape [4, 4]: |
| 1939 | |
| 1940 | unsqueeze(input, 0) will produce an output of shape [1, 4, 4], |
| 1941 | unsqueeze(input, 1) will produce an output of shape [4, 1, 4], |
| 1942 | unsqueeze(input, -1) will produce an output of shape [4, 4, 1], |
| 1943 | unsqueeze(input, -2) will produce an output of shape [4, 1, 4], |
| 1944 | |
| 1945 | Parameters: |
| 1946 | input : Tensor |
| 1947 | The input tensor to expand with a singleton dimension. |
| 1948 | |
| 1949 | axis : int |
| 1950 | The index of the singleton dimension in the output tensor. |
| 1951 | |
| 1952 | Returns: |
| 1953 | The tensor produced by the layer. |
| 1954 | ''' |
| 1955 | if axis < 0: |
| 1956 | axis = axis + input.ndim() + 1 |
| 1957 | |
| 1958 | return expand_dims(input, axis) |
| 1959 | |
| 1960 | |
| 1961 | def stack(inputs: Sequence[Tensor], dim: int = 0) -> Tensor: |
no test coverage detected