Expand#

Expand - 13#

Version

  • name: Expand (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

Broadcast the input tensor following the given shape and the broadcast rule. The broadcast rule is similar to numpy.array(input) * numpy.ones(shape): Dimensions are right alignment; Two corresponding dimensions must have the same value, or one of them is equal to 1. Also, this operator is similar to numpy.broadcast_to(input, shape), but the major difference is numpy.broadcast_to() does not allow shape to be smaller than input.size(). It is possible that the output.shape is not equal to shape, when some dimensions in shape is equal to 1, or the shape.ndim < input.shape.ndim.

Inputs

  • input (heterogeneous) - T: Input tensor

  • shape (heterogeneous) - tensor(int64): A 1-D tensor indicates the shape you want to expand to, following the broadcast rule

Outputs

  • output (heterogeneous) - T: Output tensor

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

Examples

_dim_changed

node = onnx.helper.make_node(
    'Expand',
    inputs=['data', 'new_shape'],
    outputs=['expanded'],
)
shape = [3, 1]
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[1.], [2.], [3.]]
new_shape = [2, 1, 6]
expanded = data * np.ones(new_shape, dtype=np.float32)
#print(expanded)
#[[[1., 1., 1., 1., 1., 1.],
#  [2., 2., 2., 2., 2., 2.],
#  [3., 3., 3., 3., 3., 3.]],
#
# [[1., 1., 1., 1., 1., 1.],
#  [2., 2., 2., 2., 2., 2.],
#  [3., 3., 3., 3., 3., 3.]]]
new_shape = np.array(new_shape, dtype=np.int64)
expect(node, inputs=[data, new_shape], outputs=[expanded],
       name='test_expand_dim_changed')

_dim_unchanged

node = onnx.helper.make_node(
    'Expand',
    inputs=['data', 'new_shape'],
    outputs=['expanded'],
)
shape = [3, 1]
new_shape = [3, 4]
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[1.], [2.], [3.]]
expanded = np.tile(data, 4)
#print(expanded)
#[[1., 1., 1., 1.],
# [2., 2., 2., 2.],
# [3., 3., 3., 3.]]
new_shape = np.array(new_shape, dtype=np.int64)
expect(node, inputs=[data, new_shape], outputs=[expanded],
       name='test_expand_dim_unchanged')

Differences

00Broadcast the input tensor following the given shape and the broadcast rule.Broadcast the input tensor following the given shape and the broadcast rule.
11The broadcast rule is similar to numpy.array(input) * numpy.ones(shape):The broadcast rule is similar to numpy.array(input) * numpy.ones(shape):
22Dimensions are right alignment;Dimensions are right alignment;
33Two corresponding dimensions must have the same value, or one of them is equal to 1.Two corresponding dimensions must have the same value, or one of them is equal to 1.
44Also, this operator is similar to numpy.broadcast_to(input, shape),Also, this operator is similar to numpy.broadcast_to(input, shape),
55but the major difference is numpy.broadcast_to() does not allow shape to be smaller than input.size().but the major difference is numpy.broadcast_to() does not allow shape to be smaller than input.size().
66It is possible that the output.shape is not equal to shape, when some dimensions in shape is equal to 1,It is possible that the output.shape is not equal to shape, when some dimensions in shape is equal to 1,
77or the shape.ndim < input.shape.ndim.or the shape.ndim < input.shape.ndim.
88
99**Inputs****Inputs**
1010
1111* **input** (heterogeneous) - **T**:* **input** (heterogeneous) - **T**:
1212 Input tensor Input tensor
1313* **shape** (heterogeneous) - **tensor(int64)**:* **shape** (heterogeneous) - **tensor(int64)**:
1414 A 1-D tensor indicates the shape you want to expand to, following A 1-D tensor indicates the shape you want to expand to, following
1515 the broadcast rule the broadcast rule
1616
1717**Outputs****Outputs**
1818
1919* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
2020 Output tensor Output tensor
2121
2222**Type Constraints****Type Constraints**
2323
2424* **T** in (* **T** in (
25 tensor(bfloat16),
2526 tensor(bool), tensor(bool),
2627 tensor(complex128), tensor(complex128),
2728 tensor(complex64), tensor(complex64),
2829 tensor(double), tensor(double),
2930 tensor(float), tensor(float),
3031 tensor(float16), tensor(float16),
3132 tensor(int16), tensor(int16),
3233 tensor(int32), tensor(int32),
3334 tensor(int64), tensor(int64),
3435 tensor(int8), tensor(int8),
3536 tensor(string), tensor(string),
3637 tensor(uint16), tensor(uint16),
3738 tensor(uint32), tensor(uint32),
3839 tensor(uint64), tensor(uint64),
3940 tensor(uint8) tensor(uint8)
4041 ): ):
4142 Constrain input and output types to all tensors. Constrain input and output types to all tensors.

Expand - 8#

Version

  • name: Expand (GitHub)

  • domain: main

  • since_version: 8

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

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

Summary

Broadcast the input tensor following the given shape and the broadcast rule. The broadcast rule is similar to numpy.array(input) * numpy.ones(shape): Dimensions are right alignment; Two corresponding dimensions must have the same value, or one of them is equal to 1. Also, this operator is similar to numpy.broadcast_to(input, shape), but the major difference is numpy.broadcast_to() does not allow shape to be smaller than input.size(). It is possible that the output.shape is not equal to shape, when some dimensions in shape is equal to 1, or the shape.ndim < input.shape.ndim.

Inputs

  • input (heterogeneous) - T: Input tensor

  • shape (heterogeneous) - tensor(int64): A 1-D tensor indicates the shape you want to expand to, following the broadcast rule

Outputs

  • output (heterogeneous) - T: Output tensor

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