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 dimension 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
0 | 0 | Broadcast the input tensor following the given shape and the broadcast rule. | Broadcast the input tensor following the given shape and the broadcast rule. |
1 | 1 | The broadcast rule is similar to numpy.array(input) * numpy.ones(shape): | The broadcast rule is similar to numpy.array(input) * numpy.ones(shape): |
2 | 2 | Dimensions are right alignment; | Dimensions are right alignment; |
3 | 3 | Two corresponding dimension must have the same value, or one of them is equal to 1. | Two corresponding dimension must have the same value, or one of them is equal to 1. |
4 | 4 | Also, this operator is similar to numpy.broadcast_to(input, shape), | Also, this operator is similar to numpy.broadcast_to(input, shape), |
5 | 5 | but 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(). |
6 | 6 | It 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, |
7 | 7 | or the shape.ndim < input.shape.ndim. | or the shape.ndim < input.shape.ndim. |
8 | 8 |
|
|
9 | 9 | **Inputs** | **Inputs** |
10 | 10 |
|
|
11 | 11 | * **input** (heterogeneous) - **T**: | * **input** (heterogeneous) - **T**: |
12 | 12 | Input tensor | Input tensor |
13 | 13 | * **shape** (heterogeneous) - **tensor(int64)**: | * **shape** (heterogeneous) - **tensor(int64)**: |
14 | 14 | 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 |
15 | 15 | the broadcast rule | the broadcast rule |
16 | 16 |
|
|
17 | 17 | **Outputs** | **Outputs** |
18 | 18 |
|
|
19 | 19 | * **output** (heterogeneous) - **T**: | * **output** (heterogeneous) - **T**: |
20 | 20 | Output tensor | Output tensor |
21 | 21 |
|
|
22 | 22 | **Type Constraints** | **Type Constraints** |
23 | 23 |
|
|
24 | 24 | * **T** in ( | * **T** in ( |
25 | tensor(bfloat16), | ||
25 | 26 | tensor(bool), | tensor(bool), |
26 | 27 | tensor(complex128), | tensor(complex128), |
27 | 28 | tensor(complex64), | tensor(complex64), |
28 | 29 | tensor(double), | tensor(double), |
29 | 30 | tensor(float), | tensor(float), |
30 | 31 | tensor(float16), | tensor(float16), |
31 | 32 | tensor(int16), | tensor(int16), |
32 | 33 | tensor(int32), | tensor(int32), |
33 | 34 | tensor(int64), | tensor(int64), |
34 | 35 | tensor(int8), | tensor(int8), |
35 | 36 | tensor(string), | tensor(string), |
36 | 37 | tensor(uint16), | tensor(uint16), |
37 | 38 | tensor(uint32), | tensor(uint32), |
38 | 39 | tensor(uint64), | tensor(uint64), |
39 | 40 | tensor(uint8) | tensor(uint8) |
40 | 41 | ): | ): |
41 | 42 | 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 dimension 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.