Unsqueeze - version 13#

This page documents version 13 of operator Unsqueeze. See Unsqueeze for the latest version (since version 25).

  • Domain: ai.onnx

  • Since version: 13

Insert single-dimensional entries to the shape of an input tensor (data). Takes one required input axes - which contains a list of dimension indices and this operator will insert a dimension of value 1 into the corresponding index of the output tensor (expanded).

For example:

Given an input tensor (`data`) of shape [3, 4, 5], then
Unsqueeze(data, axes=[0, 4]) outputs a tensor (`expanded`) containing the same data as `data` but with shape [1, 3, 4, 5, 1].

The axes should not contain any duplicate entries. It is an error if it contains duplicates. The rank of the output tensor (output_rank) is the rank of the input tensor (data) plus the number of values in axes. Each value in axes should be within the (inclusive) range [-output_rank , output_rank - 1]. The order of values in axes does not matter and can come in any order.

Inputs

  • data (T): Original tensor

  • axes (tensor(int64)): List of integers indicating the dimensions to be inserted. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(expanded).

Outputs

  • expanded (T): Reshaped tensor with same data as input.

Type Constraints

  • T: Constrain input and output types to all tensor types. Allowed types: tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

Examples#

test_cc_unsqueeze_axes

Node:
  Unsqueeze(data, axes) -> (expanded)
Inputs:
  data: shape=(2, 3), dtype=float32
    [[0., 1., 2.],
     [3., 4., 5.]]
  axes: shape=(2,), dtype=int64
    [0, 2]

Outputs:
  expanded: shape=(1, 2, 1, 3), dtype=float32
    [[[[0., 1., 2.]],

      [[3., 4., 5.]]]]

test_unsqueeze_axis_0

Node:
  Unsqueeze(x, axes) -> (y)
Inputs:
  x: shape=(3, 4, 5), dtype=float32
    [[[ 0.,  1.,  2.,  3.,  4.],
      [ 5.,  6.,  7.,  8.,  9.],
      [10., 11., 12., 13., 14.],
      [15., 16., 17., 18., 19.]],

     [[20., 21., 22., 23., 24.],
      [25., 26., 27., 28., 29.],
      [30., 31., 32., 33., 34.],
      [35., 36., 37., 38., 39.]],

     [[40., 41., 42., 43., 44.],
      [45., 46., 47., 48., 49.],
      [50., 51., 52., 53., 54.],
      [55., 56., 57., 58., 59.]]]
  axes: shape=(1,), dtype=int64
    [0]

Outputs:
  y: shape=(1, 3, 4, 5), dtype=float32
    [[[[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.],
       [10., 11., 12., 13., 14.],
       [15., 16., 17., 18., 19.]],

      [[20., 21., 22., 23., 24.],
       [25., 26., 27., 28., 29.],
       [30., 31., 32., 33., 34.],
       [35., 36., 37., 38., 39.]],

      [[40., 41., 42., 43., 44.],
       [45., 46., 47., 48., 49.],
       [50., 51., 52., 53., 54.],
       [55., 56., 57., 58., 59.]]]]

test_unsqueeze_axis_1

Node:
  Unsqueeze(x, axes) -> (y)
Inputs:
  x: shape=(3, 4, 5), dtype=float32
    [[[ 0.,  1.,  2.,  3.,  4.],
      [ 5.,  6.,  7.,  8.,  9.],
      [10., 11., 12., 13., 14.],
      [15., 16., 17., 18., 19.]],

     [[20., 21., 22., 23., 24.],
      [25., 26., 27., 28., 29.],
      [30., 31., 32., 33., 34.],
      [35., 36., 37., 38., 39.]],

     [[40., 41., 42., 43., 44.],
      [45., 46., 47., 48., 49.],
      [50., 51., 52., 53., 54.],
      [55., 56., 57., 58., 59.]]]
  axes: shape=(1,), dtype=int64
    [1]

Outputs:
  y: shape=(3, 1, 4, 5), dtype=float32
    [[[[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.],
       [10., 11., 12., 13., 14.],
       [15., 16., 17., 18., 19.]]],


     [[[20., 21., 22., 23., 24.],
       [25., 26., 27., 28., 29.],
       [30., 31., 32., 33., 34.],
       [35., 36., 37., 38., 39.]]],


     [[[40., 41., 42., 43., 44.],
       [45., 46., 47., 48., 49.],
       [50., 51., 52., 53., 54.],
       [55., 56., 57., 58., 59.]]]]

test_unsqueeze_axis_2

Node:
  Unsqueeze(x, axes) -> (y)
Inputs:
  x: shape=(3, 4, 5), dtype=float32
    [[[ 0.,  1.,  2.,  3.,  4.],
      [ 5.,  6.,  7.,  8.,  9.],
      [10., 11., 12., 13., 14.],
      [15., 16., 17., 18., 19.]],

     [[20., 21., 22., 23., 24.],
      [25., 26., 27., 28., 29.],
      [30., 31., 32., 33., 34.],
      [35., 36., 37., 38., 39.]],

     [[40., 41., 42., 43., 44.],
      [45., 46., 47., 48., 49.],
      [50., 51., 52., 53., 54.],
      [55., 56., 57., 58., 59.]]]
  axes: shape=(1,), dtype=int64
    [2]

Outputs:
  y: shape=(3, 4, 1, 5), dtype=float32
    [[[[ 0.,  1.,  2.,  3.,  4.]],

      [[ 5.,  6.,  7.,  8.,  9.]],

      [[10., 11., 12., 13., 14.]],

      [[15., 16., 17., 18., 19.]]],


     [[[20., 21., 22., 23., 24.]],

      [[25., 26., 27., 28., 29.]],

      [[30., 31., 32., 33., 34.]],

      [[35., 36., 37., 38., 39.]]],


     [[[40., 41., 42., 43., 44.]],

      [[45., 46., 47., 48., 49.]],

      [[50., 51., 52., 53., 54.]],

      [[55., 56., 57., 58., 59.]]]]

test_unsqueeze_negative_axes

Node:
  Unsqueeze(x, axes) -> (y)
Inputs:
  x: shape=(1, 3, 1, 5), dtype=float32
    [[[[ 0.,  1.,  2.,  3.,  4.]],

      [[ 5.,  6.,  7.,  8.,  9.]],

      [[10., 11., 12., 13., 14.]]]]
  axes: shape=(1,), dtype=int64
    [-2]

Outputs:
  y: shape=(1, 3, 1, 1, 5), dtype=float32
    [[[[[ 0.,  1.,  2.,  3.,  4.]]],


      [[[ 5.,  6.,  7.,  8.,  9.]]],


      [[[10., 11., 12., 13., 14.]]]]]

test_unsqueeze_three_axes

Node:
  Unsqueeze(x, axes) -> (y)
Inputs:
  x: shape=(3, 4, 5), dtype=float32
    [[[ 0.,  1.,  2.,  3.,  4.],
      [ 5.,  6.,  7.,  8.,  9.],
      [10., 11., 12., 13., 14.],
      [15., 16., 17., 18., 19.]],

     [[20., 21., 22., 23., 24.],
      [25., 26., 27., 28., 29.],
      [30., 31., 32., 33., 34.],
      [35., 36., 37., 38., 39.]],

     [[40., 41., 42., 43., 44.],
      [45., 46., 47., 48., 49.],
      [50., 51., 52., 53., 54.],
      [55., 56., 57., 58., 59.]]]
  axes: shape=(3,), dtype=int64
    [2, 4, 5]

Outputs:
  y: shape=(3, 4, 1, 5, 1, 1), dtype=float32
    [[[[[[ 0.]],

        [[ 1.]],

        [[ 2.]],

        [[ 3.]],

        [[ 4.]]]],



      [[[[ 5.]],

        [[ 6.]],

        [[ 7.]],

        [[ 8.]],

        [[ 9.]]]],



      [[[[10.]],

        [[11.]],

        [[12.]],

        [[13.]],

        [[14.]]]],



      [[[[15.]],

        [[16.]],

        [[17.]],

        [[18.]],

        [[19.]]]]],




     [[[[[20.]],

        [[21.]],

        [[22.]],

        [[23.]],

        [[24.]]]],



      [[[[25.]],

        [[26.]],

        [[27.]],

        [[28.]],

        [[29.]]]],



      [[[[30.]],

        [[31.]],

        [[32.]],

        [[33.]],

        [[34.]]]],



      [[[[35.]],

        [[36.]],

        [[37.]],

        [[38.]],

        [[39.]]]]],




     [[[[[40.]],

        [[41.]],

        [[42.]],

        [[43.]],

        [[44.]]]],



      [[[[45.]],

        [[46.]],

        [[47.]],

        [[48.]],

        [[49.]]]],



      [[[[50.]],

        [[51.]],

        [[52.]],

        [[53.]],

        [[54.]]]],



      [[[[55.]],

        [[56.]],

        [[57.]],

        [[58.]],

        [[59.]]]]]]

test_unsqueeze_two_axes

Node:
  Unsqueeze(x, axes) -> (y)
Inputs:
  x: shape=(3, 4, 5), dtype=float32
    [[[ 0.,  1.,  2.,  3.,  4.],
      [ 5.,  6.,  7.,  8.,  9.],
      [10., 11., 12., 13., 14.],
      [15., 16., 17., 18., 19.]],

     [[20., 21., 22., 23., 24.],
      [25., 26., 27., 28., 29.],
      [30., 31., 32., 33., 34.],
      [35., 36., 37., 38., 39.]],

     [[40., 41., 42., 43., 44.],
      [45., 46., 47., 48., 49.],
      [50., 51., 52., 53., 54.],
      [55., 56., 57., 58., 59.]]]
  axes: shape=(2,), dtype=int64
    [1, 4]

Outputs:
  y: shape=(3, 1, 4, 5, 1), dtype=float32
    [[[[[ 0.],
        [ 1.],
        [ 2.],
        [ 3.],
        [ 4.]],

       [[ 5.],
        [ 6.],
        [ 7.],
        [ 8.],
        [ 9.]],

       [[10.],
        [11.],
        [12.],
        [13.],
        [14.]],

       [[15.],
        [16.],
        [17.],
        [18.],
        [19.]]]],



     [[[[20.],
        [21.],
        [22.],
        [23.],
        [24.]],

       [[25.],
        [26.],
        [27.],
        [28.],
        [29.]],

       [[30.],
        [31.],
        [32.],
        [33.],
        [34.]],

       [[35.],
        [36.],
        [37.],
        [38.],
        [39.]]]],



     [[[[40.],
        [41.],
        [42.],
        [43.],
        [44.]],

       [[45.],
        [46.],
        [47.],
        [48.],
        [49.]],

       [[50.],
        [51.],
        [52.],
        [53.],
        [54.]],

       [[55.],
        [56.],
        [57.],
        [58.],
        [59.]]]]]

test_unsqueeze_unsorted_axes

Node:
  Unsqueeze(x, axes) -> (y)
Inputs:
  x: shape=(3, 4, 5), dtype=float32
    [[[ 0.,  1.,  2.,  3.,  4.],
      [ 5.,  6.,  7.,  8.,  9.],
      [10., 11., 12., 13., 14.],
      [15., 16., 17., 18., 19.]],

     [[20., 21., 22., 23., 24.],
      [25., 26., 27., 28., 29.],
      [30., 31., 32., 33., 34.],
      [35., 36., 37., 38., 39.]],

     [[40., 41., 42., 43., 44.],
      [45., 46., 47., 48., 49.],
      [50., 51., 52., 53., 54.],
      [55., 56., 57., 58., 59.]]]
  axes: shape=(3,), dtype=int64
    [5, 4, 2]

Outputs:
  y: shape=(3, 4, 1, 5, 1, 1), dtype=float32
    [[[[[[ 0.]],

        [[ 1.]],

        [[ 2.]],

        [[ 3.]],

        [[ 4.]]]],



      [[[[ 5.]],

        [[ 6.]],

        [[ 7.]],

        [[ 8.]],

        [[ 9.]]]],



      [[[[10.]],

        [[11.]],

        [[12.]],

        [[13.]],

        [[14.]]]],



      [[[[15.]],

        [[16.]],

        [[17.]],

        [[18.]],

        [[19.]]]]],




     [[[[[20.]],

        [[21.]],

        [[22.]],

        [[23.]],

        [[24.]]]],



      [[[[25.]],

        [[26.]],

        [[27.]],

        [[28.]],

        [[29.]]]],



      [[[[30.]],

        [[31.]],

        [[32.]],

        [[33.]],

        [[34.]]]],



      [[[[35.]],

        [[36.]],

        [[37.]],

        [[38.]],

        [[39.]]]]],




     [[[[[40.]],

        [[41.]],

        [[42.]],

        [[43.]],

        [[44.]]]],



      [[[[45.]],

        [[46.]],

        [[47.]],

        [[48.]],

        [[49.]]]],



      [[[[50.]],

        [[51.]],

        [[52.]],

        [[53.]],

        [[54.]]]],



      [[[[55.]],

        [[56.]],

        [[57.]],

        [[58.]],

        [[59.]]]]]]

Differences with previous version (11)#

SchemaDiff: Unsqueeze (domain 'ai.onnx')

  • old version: 11

  • new version: 13

  • breaking: yes

Breaking reasons:

  • input ‘axes’ (added): at position 1; option=Single; type_str=’tensor(int64)’

  • attribute ‘axes’ (removed): type=INTS; required=True

Inputs:

  • [BREAKING] added ‘axes’: at position 1; option=Single; type_str=’tensor(int64)’

Attributes:

  • [BREAKING] removed ‘axes’: type=INTS; required=True

Type constraints:

  • changed ‘T’: added types: [‘tensor(bfloat16)’]

Documentation:

  • line similarity: 0.85 (+2/-2 lines)

--- Unsqueeze v11
+++ Unsqueeze v13
@@ -1,12 +1,12 @@

 Insert single-dimensional entries to the shape of an input tensor (`data`).
-Takes one required argument `axes` - which contains a list of dimension indices and this operator will insert a dimension of value `1` into the corresponding index of the output tensor (`expanded`).
+Takes one required input `axes` - which contains a list of dimension indices and this operator will insert a dimension of value `1` into the corresponding index of the output tensor (`expanded`).

 For example:
   Given an input tensor (`data`) of shape [3, 4, 5], then
   Unsqueeze(data, axes=[0, 4]) outputs a tensor (`expanded`) containing the same data as `data` but with shape [1, 3, 4, 5, 1].

-The attribute `axes` should not contain any duplicate entries. It is an error if it contains duplicates.
+The `axes` should not contain any duplicate entries. It is an error if it contains duplicates.
 The rank of the output tensor (`output_rank`) is the rank of the input tensor (`data`) plus the number of values in `axes`.
 Each value in `axes` should be within the (inclusive) range [-output_rank , output_rank - 1].
 The order of values in `axes` does not matter and can come in any order.