Unsqueeze#

Unsqueeze - 13#

Version

  • name: Unsqueeze (GitHub)

  • domain: main

  • since_version: 13

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 13.

Summary

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 same data as data but with shape [1, 3, 4, 5, 1].

The input 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 (heterogeneous) - T: Original tensor

  • axes (heterogeneous) - 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 (heterogeneous) - T: Reshaped tensor with same data as input.

Type Constraints

  • T in ( 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) ): Constrain input and output types to all tensor types.

Examples

_unsqueeze_one_axis

x = np.random.randn(3, 4, 5).astype(np.float32)

for i in range(x.ndim):
    axes = np.array([i]).astype(np.int64)
    node = onnx.helper.make_node(
        "Unsqueeze",
        inputs=["x", "axes"],
        outputs=["y"],
    )
    y = np.expand_dims(x, axis=i)

    expect(
        node,
        inputs=[x, axes],
        outputs=[y],
        name="test_unsqueeze_axis_" + str(i),
    )

_unsqueeze_two_axes

x = np.random.randn(3, 4, 5).astype(np.float32)
axes = np.array([1, 4]).astype(np.int64)

node = onnx.helper.make_node(
    "Unsqueeze",
    inputs=["x", "axes"],
    outputs=["y"],
)
y = np.expand_dims(x, axis=1)
y = np.expand_dims(y, axis=4)

expect(node, inputs=[x, axes], outputs=[y], name="test_unsqueeze_two_axes")

_unsqueeze_three_axes

x = np.random.randn(3, 4, 5).astype(np.float32)
axes = np.array([2, 4, 5]).astype(np.int64)

node = onnx.helper.make_node(
    "Unsqueeze",
    inputs=["x", "axes"],
    outputs=["y"],
)
y = np.expand_dims(x, axis=2)
y = np.expand_dims(y, axis=4)
y = np.expand_dims(y, axis=5)

expect(node, inputs=[x, axes], outputs=[y], name="test_unsqueeze_three_axes")

_unsqueeze_unsorted_axes

x = np.random.randn(3, 4, 5).astype(np.float32)
axes = np.array([5, 4, 2]).astype(np.int64)

node = onnx.helper.make_node(
    "Unsqueeze",
    inputs=["x", "axes"],
    outputs=["y"],
)
y = np.expand_dims(x, axis=2)
y = np.expand_dims(y, axis=4)
y = np.expand_dims(y, axis=5)

expect(node, inputs=[x, axes], outputs=[y], name="test_unsqueeze_unsorted_axes")

_unsqueeze_negative_axes

node = onnx.helper.make_node(
    "Unsqueeze",
    inputs=["x", "axes"],
    outputs=["y"],
)
x = np.random.randn(1, 3, 1, 5).astype(np.float32)
axes = np.array([-2]).astype(np.int64)
y = np.expand_dims(x, axis=-2)
expect(node, inputs=[x, axes], outputs=[y], name="test_unsqueeze_negative_axes")

Differences

00Insert single-dimensional entries to the shape of an input tensor (data).Insert single-dimensional entries to the shape of an input tensor (data).
11Takes 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).
22
33For example:For example:
44 Given an input tensor (data) of shape [3, 4, 5], then Given an input tensor (data) of shape [3, 4, 5], then
55 Unsqueeze(data, axes=[0, 4]) outputs a tensor (expanded) containing same data as data but with shape [1, 3, 4, 5, 1]. Unsqueeze(data, axes=[0, 4]) outputs a tensor (expanded) containing same data as data but with shape [1, 3, 4, 5, 1].
66
77The attribute axes should not contain any duplicate entries. It is an error if it contains duplicates.The input axes should not contain any duplicate entries. It is an error if it contains duplicates.
88The rank of the output tensor (output_rank) is the rank of the input tensor (data) plus the number of values in axes.The rank of the output tensor (output_rank) is the rank of the input tensor (data) plus the number of values in axes.
99Each value in axes should be within the (inclusive) range [-output_rank , output_rank - 1].Each value in axes should be within the (inclusive) range [-output_rank , output_rank - 1].
1010The order of values in axes does not matter and can come in any order.The order of values in axes does not matter and can come in any order.
1111
12**Inputs**
13
1214**Attributes*** **data** (heterogeneous) - **T**:
13
15 Original tensor
1416* **axes** (required):* **axes** (heterogeneous) - **tensor(int64)**:
1517 List of integers indicating the dimensions to be inserted. Negative List of integers indicating the dimensions to be inserted. Negative
1618 value means counting dimensions from the back. Accepted range is value means counting dimensions from the back. Accepted range is
1719 [-r, r-1] where r = rank(expanded). [-r, r-1] where r = rank(expanded).
1820
19**Inputs**
20
21* **data** (heterogeneous) - **T**:
22 Original tensor
23
2421**Outputs****Outputs**
2522
2623* **expanded** (heterogeneous) - **T**:* **expanded** (heterogeneous) - **T**:
2724 Reshaped tensor with same data as input. Reshaped tensor with same data as input.
2825
2926**Type Constraints****Type Constraints**
3027
3128* **T** in (* **T** in (
29 tensor(bfloat16),
3230 tensor(bool), tensor(bool),
3331 tensor(complex128), tensor(complex128),
3432 tensor(complex64), tensor(complex64),
3533 tensor(double), tensor(double),
3634 tensor(float), tensor(float),
3735 tensor(float16), tensor(float16),
3836 tensor(int16), tensor(int16),
3937 tensor(int32), tensor(int32),
4038 tensor(int64), tensor(int64),
4139 tensor(int8), tensor(int8),
4240 tensor(string), tensor(string),
4341 tensor(uint16), tensor(uint16),
4442 tensor(uint32), tensor(uint32),
4543 tensor(uint64), tensor(uint64),
4644 tensor(uint8) tensor(uint8)
4745 ): ):
4846 Constrain input and output types to all tensor types. Constrain input and output types to all tensor types.

Unsqueeze - 11#

Version

  • name: Unsqueeze (GitHub)

  • domain: main

  • since_version: 11

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 11.

Summary

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).

For example:

Given an input tensor (data) of shape [3, 4, 5], then Unsqueeze(data, axes=[0, 4]) outputs a tensor (expanded) containing 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 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.

Attributes

  • axes (required): 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).

Inputs

  • data (heterogeneous) - T: Original tensor

Outputs

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

Type Constraints

  • T in ( 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) ): Constrain input and output types to all tensor types.

Differences

00Insert single-dimensional entries to the shape of a tensor.Insert single-dimensional entries to the shape of an input tensor (data).
11Takes one required argument axes, a list of dimensions that will be inserted.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).
2
23Dimension indices in axes are as seen in the output tensor. For example:For example:
34 Given a tensor such that tensor with shape [3, 4, 5], then Given an input tensor (data) of shape [3, 4, 5], then
5 Unsqueeze(data, axes=[0, 4]) outputs a tensor (expanded) containing same data as data but with shape [1, 3, 4, 5, 1].
6
7The attribute axes should not contain any duplicate entries. It is an error if it contains duplicates.
48 Unsqueeze(tensor, axes=[0, 4]) has shape [1, 3, 4, 5, 1]The rank of the output tensor (output_rank) is the rank of the input tensor (data) plus the number of values in axes.
9Each value in axes should be within the (inclusive) range [-output_rank , output_rank - 1].
10The order of values in axes does not matter and can come in any order.
511
612**Attributes****Attributes**
713
814* **axes** (required):* **axes** (required):
915 List of non-negative integers, indicate the dimensions to be List of integers indicating the dimensions to be inserted. Negative
10 inserted
16 value means counting dimensions from the back. Accepted range is
17 [-r, r-1] where r = rank(expanded).
1118
1219**Inputs****Inputs**
1320
1421* **data** (heterogeneous) - **T**:* **data** (heterogeneous) - **T**:
1522 Original tensor Original tensor
1623
1724**Outputs****Outputs**
1825
1926* **expanded** (heterogeneous) - **T**:* **expanded** (heterogeneous) - **T**:
2027 Reshaped tensor with same data as input. Reshaped tensor with same data as input.
2128
2229**Type Constraints****Type Constraints**
2330
2431* **T** in (* **T** in (
2532 tensor(bool), tensor(bool),
2633 tensor(complex128), tensor(complex128),
2734 tensor(complex64), tensor(complex64),
2835 tensor(double), tensor(double),
2936 tensor(float), tensor(float),
3037 tensor(float16), tensor(float16),
3138 tensor(int16), tensor(int16),
3239 tensor(int32), tensor(int32),
3340 tensor(int64), tensor(int64),
3441 tensor(int8), tensor(int8),
3542 tensor(string), tensor(string),
3643 tensor(uint16), tensor(uint16),
3744 tensor(uint32), tensor(uint32),
3845 tensor(uint64), tensor(uint64),
3946 tensor(uint8) tensor(uint8)
4047 ): ):
4148 Constrain input and output types to all tensor types. Constrain input and output types to all tensor types.

Unsqueeze - 1#

Version

  • name: Unsqueeze (GitHub)

  • domain: main

  • since_version: 1

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 1.

Summary

Insert single-dimensional entries to the shape of a tensor. Takes one required argument axes, a list of dimensions that will be inserted. Dimension indices in axes are as seen in the output tensor. For example:

Given a tensor such that tensor with shape [3, 4, 5], then Unsqueeze(tensor, axes=[0, 4]) has shape [1, 3, 4, 5, 1]

Attributes

  • axes (required): List of non-negative integers, indicate the dimensions to be inserted

Inputs

  • data (heterogeneous) - T: Original tensor

Outputs

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

Type Constraints

  • T in ( 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) ): Constrain input and output types to all tensor types.