giagrad.Tensor.unsqueeze#

Tensor.unsqueeze(axis)[source]#

Returns a new tensor with its shape expanded.

unsqueeze inserts a new axis of size one in the specified axis. For example a tensor of shape \((N_1, N_2, N_3)\) with \(\text{axis}=(0, 2)\) will output a tensor of shape \((1, N_1, 1, N_2, N_3)\).

Note

The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other.

Parameters:

axis ((int, ...) or int, optional) – Axis to be inserted with size one in the output tensor.

Examples

>>> t = Tensor.empty(2, 2, 2).uniform()
>>> t
tensor: [[[0.54203224 0.4911729 ]
          [0.29304293 0.9672827 ]]
...
         [[0.18163016 0.34806943]
          [0.30323076 0.3647484 ]]]
>>> t.unsqueeze(axis=(0,2))
tensor: [[[[[0.54203224 0.4911729 ]
            [0.29304293 0.9672827 ]]]
...
...
          [[[0.18163016 0.34806943]
            [0.30323076 0.3647484 ]]]]] fn: UnSqueeze(axis = (0, 2))
>>> t.unsqueeze(axis=(0,2)).shape
(1, 2, 1, 2, 2)