# Test Coverage Report (ONNX Core Operators)
## Outlines
* [Node Test Coverage](#node-test-coverage)
* [Model Test Coverage](#model-test-coverage)
* [Overall Test Coverage](#overall-test-coverage)
# Node Test Coverage
## Summary
Node tests have covered 136/151 (90.07%, 5 generators excluded) common operators.
Node tests have covered 0/0 (N/A) experimental operators.
* [Covered Common Operators](#covered-common-operators)
* [No Cover Common Operators](#no-cover-common-operators)
* [Covered Experimental Operators](#covered-experimental-operators)
* [No Cover Experimental Operators](#no-cover-experimental-operators)
## 💚Covered Common Operators
### Abs
There are 1 test cases, listed as following:
abs
```python
node = onnx.helper.make_node(
'Abs',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = abs(x)
expect(node, inputs=[x], outputs=[y],
name='test_abs')
```
### Acos
There are 1 test cases, listed as following:
acos
```python
node = onnx.helper.make_node(
'Acos',
inputs=['x'],
outputs=['y'],
)
x = np.array([-0.5, 0, 0.5]).astype(np.float32)
y = np.arccos(x)
expect(node, inputs=[x], outputs=[y],
name='test_acos_example')
x = np.random.rand(3, 4, 5).astype(np.float32)
y = np.arccos(x)
expect(node, inputs=[x], outputs=[y],
name='test_acos')
```
### Acosh
There are 1 test cases, listed as following:
acosh
```python
node = onnx.helper.make_node(
'Acosh',
inputs=['x'],
outputs=['y'],
)
x = np.array([10, np.e, 1]).astype(np.float32)
y = np.arccosh(x) # expected output [2.99322295, 1.65745449, 0.]
expect(node, inputs=[x], outputs=[y],
name='test_acosh_example')
x = np.random.uniform(1.0, 10.0, (3, 4, 5)).astype(np.float32)
y = np.arccosh(x)
expect(node, inputs=[x], outputs=[y],
name='test_acosh')
```
### Add
There are 2 test cases, listed as following:
add
```python
node = onnx.helper.make_node(
'Add',
inputs=['x', 'y'],
outputs=['sum'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(3, 4, 5).astype(np.float32)
expect(node, inputs=[x, y], outputs=[x + y],
name='test_add')
```
add_broadcast
```python
node = onnx.helper.make_node(
'Add',
inputs=['x', 'y'],
outputs=['sum'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(5).astype(np.float32)
expect(node, inputs=[x, y], outputs=[x + y],
name='test_add_bcast')
```
### And
There are 2 test cases, listed as following:
and
```python
node = onnx.helper.make_node(
'And',
inputs=['x', 'y'],
outputs=['and'],
)
# 2d
x = (np.random.randn(3, 4) > 0).astype(np.bool)
y = (np.random.randn(3, 4) > 0).astype(np.bool)
z = np.logical_and(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_and2d')
# 3d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
z = np.logical_and(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_and3d')
# 4d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
z = np.logical_and(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_and4d')
```
and_broadcast
```python
node = onnx.helper.make_node(
'And',
inputs=['x', 'y'],
outputs=['and'],
)
# 3d vs 1d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(5) > 0).astype(np.bool)
z = np.logical_and(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_and_bcast3v1d')
# 3d vs 2d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(4, 5) > 0).astype(np.bool)
z = np.logical_and(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_and_bcast3v2d')
# 4d vs 2d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(5, 6) > 0).astype(np.bool)
z = np.logical_and(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_and_bcast4v2d')
# 4d vs 3d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(4, 5, 6) > 0).astype(np.bool)
z = np.logical_and(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_and_bcast4v3d')
# 4d vs 4d
x = (np.random.randn(1, 4, 1, 6) > 0).astype(np.bool)
y = (np.random.randn(3, 1, 5, 6) > 0).astype(np.bool)
z = np.logical_and(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_and_bcast4v4d')
```
### ArgMax
There are 4 test cases, listed as following:
default_axes_keepdims
```python
data = np.array([[2, 1], [3, 10]], dtype=np.float32)
keepdims = 1
node = onnx.helper.make_node(
'ArgMax',
inputs=['data'],
outputs=['result'],
keepdims=keepdims)
# result: [[1], [1]]
result = argmax_use_numpy(data, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example')
data = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)
# result's shape: [1, 3, 4]
result = argmax_use_numpy(data, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random')
```
keepdims
```python
data = np.array([[2, 1], [3, 10]], dtype=np.float32)
axis = 1
keepdims = 1
node = onnx.helper.make_node(
'ArgMax',
inputs=['data'],
outputs=['result'],
axis=axis,
keepdims=keepdims)
# result: [[0], [1]]
result = argmax_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example')
data = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)
# result's shape: [2, 1, 4]
result = argmax_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random')
```
negative_axis_keepdims
```python
data = np.array([[2, 1], [3, 10]], dtype=np.float32)
axis = -1
keepdims = 1
node = onnx.helper.make_node(
'ArgMax',
inputs=['data'],
outputs=['result'],
axis=axis,
keepdims=keepdims)
# result: [[0], [1]]
result = argmax_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example')
data = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)
# result's shape: [2, 3, 1]
result = argmax_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random')
```
no_keepdims
```python
data = np.array([[2, 1], [3, 10]], dtype=np.float32)
axis = 1
keepdims = 0
node = onnx.helper.make_node(
'ArgMax',
inputs=['data'],
outputs=['result'],
axis=axis,
keepdims=keepdims)
# result: [[0, 1]]
result = argmax_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example')
data = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)
# result's shape: [2, 4]
result = argmax_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random')
```
### ArgMin
There are 4 test cases, listed as following:
default_axes_keepdims
```python
data = np.array([[2, 1], [3, 10]], dtype=np.float32)
keepdims = 1
node = onnx.helper.make_node(
'ArgMin',
inputs=['data'],
outputs=['result'],
keepdims=keepdims)
# The content of result is : [[0], [0]]
result = argmin_use_numpy(data, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example')
data = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)
# result's shape: [1, 3, 4]
result = argmin_use_numpy(data, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random')
```
keepdims
```python
data = np.array([[2, 1], [3, 10]], dtype=np.float32)
axis = 1
keepdims = 1
node = onnx.helper.make_node(
'ArgMin',
inputs=['data'],
outputs=['result'],
axis=axis,
keepdims=keepdims)
# The content of result is : [[1], [0]]
result = argmin_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example')
data = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)
# result's shape: [2, 1, 4]
result = argmin_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random')
```
negative_axis_keepdims
```python
data = np.array([[2, 1], [3, 10]], dtype=np.float32)
axis = -1
keepdims = 1
node = onnx.helper.make_node(
'ArgMin',
inputs=['data'],
outputs=['result'],
axis=axis,
keepdims=keepdims)
# The content of result is : [[1], [0]]
result = argmin_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example')
data = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)
# result's shape: [2, 3, 1]
result = argmin_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random')
```
no_keepdims
```python
data = np.array([[2, 1], [3, 10]], dtype=np.float32)
axis = 1
keepdims = 0
node = onnx.helper.make_node(
'ArgMin',
inputs=['data'],
outputs=['result'],
axis=axis,
keepdims=keepdims)
# The content of result is : [[1, 0]]
result = argmin_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example')
data = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)
# result's shape: [2, 4]
result = argmin_use_numpy(data, axis=axis, keepdims=keepdims)
expect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random')
```
### Asin
There are 1 test cases, listed as following:
asin
```python
node = onnx.helper.make_node(
'Asin',
inputs=['x'],
outputs=['y'],
)
x = np.array([-0.5, 0, 0.5]).astype(np.float32)
y = np.arcsin(x)
expect(node, inputs=[x], outputs=[y],
name='test_asin_example')
x = np.random.rand(3, 4, 5).astype(np.float32)
y = np.arcsin(x)
expect(node, inputs=[x], outputs=[y],
name='test_asin')
```
### Asinh
There are 1 test cases, listed as following:
asinh
```python
node = onnx.helper.make_node(
'Asinh',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.arcsinh(x) # expected output [-0.88137358, 0., 0.88137358]
expect(node, inputs=[x], outputs=[y],
name='test_asinh_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.arcsinh(x)
expect(node, inputs=[x], outputs=[y],
name='test_asinh')
```
### Atan
There are 1 test cases, listed as following:
atan
```python
node = onnx.helper.make_node(
'Atan',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.arctan(x)
expect(node, inputs=[x], outputs=[y],
name='test_atan_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.arctan(x)
expect(node, inputs=[x], outputs=[y],
name='test_atan')
```
### Atanh
There are 1 test cases, listed as following:
atanh
```python
node = onnx.helper.make_node(
'Atanh',
inputs=['x'],
outputs=['y'],
)
x = np.array([-0.5, 0, 0.5]).astype(np.float32)
y = np.arctanh(x) # expected output [-0.54930615, 0., 0.54930615]
expect(node, inputs=[x], outputs=[y],
name='test_atanh_example')
x = np.random.uniform(0.0, 1.0, (3, 4, 5)).astype(np.float32)
y = np.arctanh(x)
expect(node, inputs=[x], outputs=[y],
name='test_atanh')
```
### AveragePool
There are 13 test cases, listed as following:
averagepool_1d_default
```python
"""
input_shape: [1, 3, 32]
output_shape: [1, 3, 31]
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2],
)
x = np.random.randn(1, 3, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = [2]
strides = [1]
out_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)
padded = x
y = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'AVG')
expect(node, inputs=[x], outputs=[y], name='test_averagepool_1d_default')
```
averagepool_2d_ceil
```python
"""
input_shape: [1, 1, 4, 4]
output_shape: [1, 1, 2, 2]
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[3, 3],
strides=[2, 2],
ceil_mode=True
)
x = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]]).astype(np.float32)
y = np.array([[[
[6, 7.5],
[12, 13.5]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_ceil')
```
averagepool_2d_default
```python
"""
input_shape: [1, 3, 32, 32]
output_shape: [1, 3, 31, 31]
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (2, 2)
strides = (1, 1)
out_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)
padded = x
y = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_default')
```
averagepool_2d_pads
```python
"""
input_shape: [1, 3, 28, 28]
output_shape: [1, 3, 30, 30]
pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[3, 3],
pads=[2, 2, 2, 2]
)
x = np.random.randn(1, 3, 28, 28).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (3, 3)
strides = (1, 1)
pad_bottom = 2
pad_top = 2
pad_right = 2
pad_left = 2
pad_shape = [pad_top + pad_bottom, pad_left + pad_right]
out_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)
padded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',
constant_values=np.nan)
y = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads')
```
averagepool_2d_pads_count_include_pad
```python
"""
input_shape: [1, 3, 28, 28]
output_shape: [1, 3, 30, 30]
pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[3, 3],
pads=[2, 2, 2, 2],
count_include_pad=1,
)
x = np.random.randn(1, 3, 28, 28).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (3, 3)
strides = (1, 1)
pad_bottom = 2
pad_top = 2
pad_right = 2
pad_left = 2
pad_shape = [pad_top + pad_bottom, pad_left + pad_right]
out_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)
padded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',
constant_values=0)
y = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG', count_include_pad=1)
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads_count_include_pad')
```
averagepool_2d_precomputed_pads
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 5, 5]
pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[5, 5],
pads=[2, 2, 2, 2]
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[[7, 7.5, 8, 8.5, 9],
[9.5, 10, 10.5, 11, 11.5],
[12, 12.5, 13, 13.5, 14],
[14.5, 15, 15.5, 16, 16.5],
[17, 17.5, 18, 18.5, 19]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads')
```
averagepool_2d_precomputed_pads_count_include_pad
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 5, 5]
pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[5, 5],
pads=[2, 2, 2, 2],
count_include_pad=1
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[[2.5200, 3.6000, 4.8000, 4.0800, 3.2400],
[4.5600, 6.4000, 8.4000, 7.0400, 5.5200],
[7.2000, 10.0000, 13.0000, 10.8000, 8.4000],
[6.9600, 9.6000, 12.4000, 10.2400, 7.9200],
[6.1200, 8.4000, 10.8000, 8.8800, 6.8400]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads_count_include_pad')
```
averagepool_2d_precomputed_same_upper
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 3, 3]
pad_shape: [2, 2] -> [1, 1, 1, 1] by axis
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[3, 3],
strides=[2, 2],
auto_pad='SAME_UPPER'
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[[4, 5.5, 7],
[11.5, 13, 14.5],
[19, 20.5, 22]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_same_upper')
```
averagepool_2d_precomputed_strides
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 2, 2]
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
strides=[2, 2]
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[[4, 6],
[14, 16]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_strides')
```
averagepool_2d_same_lower
```python
"""
input_shape: [1, 3, 32, 32]
output_shape: [1, 3, 32, 32]
pad_shape: [1, 1] -> [1, 0, 1, 0] by axis
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
auto_pad='SAME_LOWER'
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (2, 2)
strides = (1, 1)
out_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)
pad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)
pad_bottom = pad_shape[0] // 2
pad_top = pad_shape[0] - pad_bottom
pad_right = pad_shape[1] // 2
pad_left = pad_shape[1] - pad_right
padded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',
constant_values=np.nan)
y = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_lower')
```
averagepool_2d_same_upper
```python
"""
input_shape: [1, 3, 32, 32]
output_shape: [1, 3, 32, 32]
pad_shape: [1, 1] -> [0, 1, 0, 1] by axis
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
auto_pad='SAME_UPPER'
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (2, 2)
strides = (1, 1)
out_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)
pad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)
pad_top = pad_shape[0] // 2
pad_bottom = pad_shape[0] - pad_top
pad_left = pad_shape[1] // 2
pad_right = pad_shape[1] - pad_left
padded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',
constant_values=np.nan)
y = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_upper')
```
averagepool_2d_strides
```python
"""
input_shape: [1, 3, 32, 32]
output_shape: [1, 3, 10, 10]
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[5, 5],
strides=[3, 3]
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (5, 5)
strides = (3, 3)
out_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)
padded = x
y = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')
expect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_strides')
```
averagepool_3d_default
```python
"""
input_shape: [1, 3, 32, 32, 32]
output_shape: [1, 3, 31, 31, 31]
"""
node = onnx.helper.make_node(
'AveragePool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2, 2],
)
x = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = [2, 2, 2]
strides = [1, 1, 1]
out_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)
padded = x
y = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'AVG')
expect(node, inputs=[x], outputs=[y], name='test_averagepool_3d_default')
```
### BatchNormalization
There are 1 test cases, listed as following:
batchnormalization
```python
def _batchnorm_test_mode(x, s, bias, mean, var, epsilon=1e-5): # type: ignore
dims_x = len(x.shape)
dim_ones = (1,) * (dims_x - 2)
s = s.reshape(-1, *dim_ones)
bias = bias.reshape(-1, *dim_ones)
mean = mean.reshape(-1, *dim_ones)
var = var.reshape(-1, *dim_ones)
return s * (x - mean) / np.sqrt(var + epsilon) + bias
# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
mean = np.array([0, 3]).astype(np.float32)
var = np.array([1, 1.5]).astype(np.float32)
y = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)
node = onnx.helper.make_node(
'BatchNormalization',
inputs=['x', 's', 'bias', 'mean', 'var'],
outputs=['y'],
)
# output size: (1, 2, 1, 3)
expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
name='test_batchnorm_example')
# input size: (2, 3, 4, 5)
x = np.random.randn(2, 3, 4, 5).astype(np.float32)
s = np.random.randn(3).astype(np.float32)
bias = np.random.randn(3).astype(np.float32)
mean = np.random.randn(3).astype(np.float32)
var = np.random.rand(3).astype(np.float32)
epsilon = 1e-2
y = _batchnorm_test_mode(x, s, bias, mean, var, epsilon).astype(np.float32)
node = onnx.helper.make_node(
'BatchNormalization',
inputs=['x', 's', 'bias', 'mean', 'var'],
outputs=['y'],
epsilon=epsilon,
)
# output size: (2, 3, 4, 5)
expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
name='test_batchnorm_epsilon')
```
### BitShift
There are 8 test cases, listed as following:
left_unit16
```python
node = onnx.helper.make_node(
'BitShift',
inputs=['x', 'y'],
outputs=['z'],
direction="LEFT"
)
x = np.array([16, 4, 1]).astype(np.uint16)
y = np.array([1, 2, 3]).astype(np.uint16)
z = x << y # expected output [32, 16, 8]
expect(node, inputs=[x, y], outputs=[z],
name='test_bitshift_left_uint16')
```
left_unit32
```python
node = onnx.helper.make_node(
'BitShift',
inputs=['x', 'y'],
outputs=['z'],
direction="LEFT"
)
x = np.array([16, 4, 1]).astype(np.uint32)
y = np.array([1, 2, 3]).astype(np.uint32)
z = x << y # expected output [32, 16, 8]
expect(node, inputs=[x, y], outputs=[z],
name='test_bitshift_left_uint32')
```
left_unit64
```python
node = onnx.helper.make_node(
'BitShift',
inputs=['x', 'y'],
outputs=['z'],
direction="LEFT"
)
x = np.array([16, 4, 1]).astype(np.uint64)
y = np.array([1, 2, 3]).astype(np.uint64)
z = x << y # expected output [32, 16, 8]
expect(node, inputs=[x, y], outputs=[z],
name='test_bitshift_left_uint64')
```
left_unit8
```python
node = onnx.helper.make_node(
'BitShift',
inputs=['x', 'y'],
outputs=['z'],
direction="LEFT"
)
x = np.array([16, 4, 1]).astype(np.uint8)
y = np.array([1, 2, 3]).astype(np.uint8)
z = x << y # expected output [32, 16, 8]
expect(node, inputs=[x, y], outputs=[z],
name='test_bitshift_left_uint8')
```
right_unit16
```python
node = onnx.helper.make_node(
'BitShift',
inputs=['x', 'y'],
outputs=['z'],
direction="RIGHT"
)
x = np.array([16, 4, 1]).astype(np.uint16)
y = np.array([1, 2, 3]).astype(np.uint16)
z = x >> y # expected output [8, 1, 0]
expect(node, inputs=[x, y], outputs=[z],
name='test_bitshift_right_uint16')
```
right_unit32
```python
node = onnx.helper.make_node(
'BitShift',
inputs=['x', 'y'],
outputs=['z'],
direction="RIGHT"
)
x = np.array([16, 4, 1]).astype(np.uint32)
y = np.array([1, 2, 3]).astype(np.uint32)
z = x >> y # expected output [8, 1, 0]
expect(node, inputs=[x, y], outputs=[z],
name='test_bitshift_right_uint32')
```
right_unit64
```python
node = onnx.helper.make_node(
'BitShift',
inputs=['x', 'y'],
outputs=['z'],
direction="RIGHT"
)
x = np.array([16, 4, 1]).astype(np.uint64)
y = np.array([1, 2, 3]).astype(np.uint64)
z = x >> y # expected output [8, 1, 0]
expect(node, inputs=[x, y], outputs=[z],
name='test_bitshift_right_uint64')
```
right_unit8
```python
node = onnx.helper.make_node(
'BitShift',
inputs=['x', 'y'],
outputs=['z'],
direction="RIGHT"
)
x = np.array([16, 4, 1]).astype(np.uint8)
y = np.array([1, 2, 3]).astype(np.uint8)
z = x >> y # expected output [8, 1, 0]
expect(node, inputs=[x, y], outputs=[z],
name='test_bitshift_right_uint8')
```
### Cast
There are 1 test cases, listed as following:
cast
```python
shape = (3, 4)
test_cases = [
('FLOAT', 'FLOAT16'),
('FLOAT', 'DOUBLE'),
('FLOAT16', 'FLOAT'),
('FLOAT16', 'DOUBLE'),
('DOUBLE', 'FLOAT'),
('DOUBLE', 'FLOAT16'),
('FLOAT', 'STRING'),
('STRING', 'FLOAT'),
]
for from_type, to_type in test_cases:
if 'STRING' != from_type:
input = np.random.random_sample(shape).astype(
TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, from_type)])
if ('STRING' == to_type):
# Converting input to str, then give it np.object dtype for generating script
ss = []
for i in input.flatten():
s = str(i).encode('utf-8')
su = s.decode('utf-8')
ss.append(su)
output = np.array(ss).astype(np.object).reshape([3, 4])
else:
output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])
else:
input = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',
u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',
u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.dtype(np.object)).reshape([3, 4])
output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])
node = onnx.helper.make_node(
'Cast',
inputs=['input'],
outputs=['output'],
to=getattr(TensorProto, to_type),
)
expect(node, inputs=[input], outputs=[output],
name='test_cast_' + from_type + '_to_' + to_type)
```
### Ceil
There are 1 test cases, listed as following:
ceil
```python
node = onnx.helper.make_node(
'Ceil',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1.5, 1.2]).astype(np.float32)
y = np.ceil(x) # expected output [-1., 2.]
expect(node, inputs=[x], outputs=[y],
name='test_ceil_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.ceil(x)
expect(node, inputs=[x], outputs=[y],
name='test_ceil')
```
### Clip
There are 2 test cases, listed as following:
clip
```python
node = onnx.helper.make_node(
'Clip',
inputs=['x', 'min', 'max'],
outputs=['y'],
)
x = np.array([-2, 0, 2]).astype(np.float32)
min_val = np.float32(-1)
max_val = np.float32(1)
y = np.clip(x, min_val, max_val) # expected output [-1., 0., 1.]
expect(node, inputs=[x, min_val, max_val], outputs=[y],
name='test_clip_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, min_val, max_val)
expect(node, inputs=[x, min_val, max_val], outputs=[y],
name='test_clip')
node = onnx.helper.make_node(
'Clip',
inputs=['x', 'min', 'max'],
outputs=['y'],
)
min_val = np.float32(-5)
max_val = np.float32(5)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.array([-1, 0, 1]).astype(np.float32)
expect(node, inputs=[x, min_val, max_val], outputs=[y],
name='test_clip_inbounds')
x = np.array([-6, 0, 6]).astype(np.float32)
y = np.array([-5, 0, 5]).astype(np.float32)
expect(node, inputs=[x, min_val, max_val], outputs=[y],
name='test_clip_outbounds')
x = np.array([-1, 0, 6]).astype(np.float32)
y = np.array([-1, 0, 5]).astype(np.float32)
expect(node, inputs=[x, min_val, max_val], outputs=[y],
name='test_clip_splitbounds')
```
clip_default
```python
node = onnx.helper.make_node(
'Clip',
inputs=['x', 'min'],
outputs=['y'],
)
min_val = np.float32(0)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, min_val, np.inf)
expect(node, inputs=[x, min_val], outputs=[y],
name='test_clip_default_min')
no_min = "" # optional input, not supplied
node = onnx.helper.make_node(
'Clip',
inputs=['x', no_min, 'max'],
outputs=['y'],
)
max_val = np.float32(0)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, -np.inf, max_val)
expect(node, inputs=[x, max_val], outputs=[y],
name='test_clip_default_max')
no_max = "" # optional input, not supplied
node = onnx.helper.make_node(
'Clip',
inputs=['x', no_min, no_max],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.array([-1, 0, 1]).astype(np.float32)
expect(node, inputs=[x], outputs=[y],
name='test_clip_default_inbounds')
```
### Compress
There are 4 test cases, listed as following:
compress_0
```python
node = onnx.helper.make_node(
'Compress',
inputs=['input', 'condition'],
outputs=['output'],
axis=0,
)
input = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)
condition = np.array([0, 1, 1])
output = np.compress(condition, input, axis=0)
#print(output)
#[[ 3. 4.]
# [ 5. 6.]]
expect(node, inputs=[input, condition.astype(np.bool)], outputs=[output],
name='test_compress_0')
```
compress_1
```python
node = onnx.helper.make_node(
'Compress',
inputs=['input', 'condition'],
outputs=['output'],
axis=1,
)
input = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)
condition = np.array([0, 1])
output = np.compress(condition, input, axis=1)
#print(output)
#[[ 2.]
# [ 4.]
# [ 6.]]
expect(node, inputs=[input, condition.astype(np.bool)], outputs=[output],
name='test_compress_1')
```
compress_default_axis
```python
node = onnx.helper.make_node(
'Compress',
inputs=['input', 'condition'],
outputs=['output'],
)
input = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)
condition = np.array([0, 1, 0, 0, 1])
output = np.compress(condition, input)
#print(output)
#[ 2., 5.]
expect(node, inputs=[input, condition.astype(np.bool)], outputs=[output],
name='test_compress_default_axis')
```
compress_negative_axis
```python
node = onnx.helper.make_node(
'Compress',
inputs=['input', 'condition'],
outputs=['output'],
axis=-1,
)
input = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)
condition = np.array([0, 1])
output = np.compress(condition, input, axis=-1)
# print(output)
#[[ 2.]
# [ 4.]
# [ 6.]]
expect(node, inputs=[input, condition.astype(np.bool)], outputs=[output],
name='test_compress_negative_axis')
```
### Concat
There are 1 test cases, listed as following:
concat
```python
test_cases = {
'1d': ([1, 2],
[3, 4]),
'2d': ([[1, 2], [3, 4]],
[[5, 6], [7, 8]]),
'3d': ([[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
[[[9, 10], [11, 12]], [[13, 14], [15, 16]]])
} # type: Dict[Text, Sequence[Any]]
for test_case, values_ in test_cases.items():
values = [np.asarray(v, dtype=np.float32) for v in values_]
for i in range(len(values[0].shape)):
in_args = ['value' + str(k) for k in range(len(values))]
node = onnx.helper.make_node(
'Concat',
inputs=[s for s in in_args],
outputs=['output'],
axis=i
)
output = np.concatenate(values, i)
expect(node, inputs=[v for v in values], outputs=[output],
name='test_concat_' + test_case + '_axis_' + str(i))
for i in range(-len(values[0].shape), 0):
in_args = ['value' + str(k) for k in range(len(values))]
node = onnx.helper.make_node(
'Concat',
inputs=[s for s in in_args],
outputs=['output'],
axis=i
)
output = np.concatenate(values, i)
expect(node, inputs=[v for v in values], outputs=[output],
name='test_concat_' + test_case + '_axis_negative_' + str(abs(i)))
```
### Constant
There are 1 test cases, listed as following:
constant
```python
values = np.random.randn(5, 5).astype(np.float32)
node = onnx.helper.make_node(
'Constant',
inputs=[],
outputs=['values'],
value=onnx.helper.make_tensor(
name='const_tensor',
data_type=onnx.TensorProto.FLOAT,
dims=values.shape,
vals=values.flatten().astype(float),
),
)
expect(node, inputs=[], outputs=[values],
name='test_constant')
```
### ConstantOfShape
There are 2 test cases, listed as following:
float_ones
```python
x = np.array([4, 3, 2]).astype(np.int64)
tensor_value = onnx.helper.make_tensor("value", onnx.TensorProto.FLOAT,
[1], [1])
node = onnx.helper.make_node(
'ConstantOfShape',
inputs=['x'],
outputs=['y'],
value=tensor_value,
)
y = np.ones(x, dtype=np.float32)
expect(node, inputs=[x], outputs=[y],
name='test_constantofshape_float_ones')
```
int32_zeros
```python
x = np.array([10, 6]).astype(np.int64)
tensor_value = onnx.helper.make_tensor("value", onnx.TensorProto.INT32,
[1], [0])
node = onnx.helper.make_node(
'ConstantOfShape',
inputs=['x'],
outputs=['y'],
value=tensor_value,
)
y = np.zeros(x, dtype=np.int32)
expect(node, inputs=[x], outputs=[y],
name='test_constantofshape_int_zeros')
```
### Conv
There are 2 test cases, listed as following:
conv
```python
x = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 5, 5) input tensor
[5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.],
[15., 16., 17., 18., 19.],
[20., 21., 22., 23., 24.]]]]).astype(np.float32)
W = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights
[1., 1., 1.],
[1., 1., 1.]]]]).astype(np.float32)
# Convolution with padding
node_with_padding = onnx.helper.make_node(
'Conv',
inputs=['x', 'W'],
outputs=['y'],
kernel_shape=[3, 3],
# Default values for other attributes: strides=[1, 1], dilations=[1, 1], groups=1
pads=[1, 1, 1, 1],
)
y_with_padding = np.array([[[[12., 21., 27., 33., 24.], # (1, 1, 5, 5) output tensor
[33., 54., 63., 72., 51.],
[63., 99., 108., 117., 81.],
[93., 144., 153., 162., 111.],
[72., 111., 117., 123., 84.]]]]).astype(np.float32)
expect(node_with_padding, inputs=[x, W], outputs=[y_with_padding],
name='test_basic_conv_with_padding')
# Convolution without padding
node_without_padding = onnx.helper.make_node(
'Conv',
inputs=['x', 'W'],
outputs=['y'],
kernel_shape=[3, 3],
# Default values for other attributes: strides=[1, 1], dilations=[1, 1], groups=1
pads=[0, 0, 0, 0],
)
y_without_padding = np.array([[[[54., 63., 72.], # (1, 1, 3, 3) output tensor
[99., 108., 117.],
[144., 153., 162.]]]]).astype(np.float32)
expect(node_without_padding, inputs=[x, W], outputs=[y_without_padding],
name='test_basic_conv_without_padding')
```
conv_with_strides
```python
x = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 7, 5) input tensor
[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.]]]]).astype(np.float32)
W = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights
[1., 1., 1.],
[1., 1., 1.]]]]).astype(np.float32)
# Convolution with strides=2 and padding
node_with_padding = onnx.helper.make_node(
'Conv',
inputs=['x', 'W'],
outputs=['y'],
kernel_shape=[3, 3],
pads=[1, 1, 1, 1],
strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1
)
y_with_padding = np.array([[[[12., 27., 24.], # (1, 1, 4, 3) output tensor
[63., 108., 81.],
[123., 198., 141.],
[112., 177., 124.]]]]).astype(np.float32)
expect(node_with_padding, inputs=[x, W], outputs=[y_with_padding],
name='test_conv_with_strides_padding')
# Convolution with strides=2 and no padding
node_without_padding = onnx.helper.make_node(
'Conv',
inputs=['x', 'W'],
outputs=['y'],
kernel_shape=[3, 3],
pads=[0, 0, 0, 0],
strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1
)
y_without_padding = np.array([[[[54., 72.], # (1, 1, 3, 2) output tensor
[144., 162.],
[234., 252.]]]]).astype(np.float32)
expect(node_without_padding, inputs=[x, W], outputs=[y_without_padding],
name='test_conv_with_strides_no_padding')
# Convolution with strides=2 and padding only along one dimension (the H dimension in NxCxHxW tensor)
node_with_asymmetric_padding = onnx.helper.make_node(
'Conv',
inputs=['x', 'W'],
outputs=['y'],
kernel_shape=[3, 3],
pads=[1, 0, 1, 0],
strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1
)
y_with_asymmetric_padding = np.array([[[[21., 33.], # (1, 1, 4, 2) output tensor
[99., 117.],
[189., 207.],
[171., 183.]]]]).astype(np.float32)
expect(node_with_asymmetric_padding, inputs=[x, W], outputs=[y_with_asymmetric_padding],
name='test_conv_with_strides_and_asymmetric_padding')
```
### ConvInteger
There are 1 test cases, listed as following:
convinteger
```python
x = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10]).astype(np.uint8).reshape((1, 1, 3, 3))
x_zero_point = np.uint8(1)
w = np.array([1, 1, 1, 1]).astype(np.uint8).reshape((1, 1, 2, 2))
y = np.array([12, 16, 24, 28]).astype(np.int32).reshape(1, 1, 2, 2)
# ConvInteger without padding
convinteger_node = onnx.helper.make_node('ConvInteger',
inputs=['x', 'w', 'x_zero_point'],
outputs=['y'])
expect(convinteger_node, inputs=[x, w, x_zero_point], outputs=[y],
name='test_basic_convinteger')
# ConvInteger with padding
y_with_padding = np.array([1, 3, 5, 3, 5, 12, 16, 9, 11, 24, 28, 15, 7, 15, 17, 9]).astype(np.int32).reshape((1, 1, 4, 4))
convinteger_node_with_padding = onnx.helper.make_node('ConvInteger',
inputs=['x', 'w', 'x_zero_point'],
outputs=['y'],
pads=[1, 1, 1, 1],)
expect(convinteger_node_with_padding, inputs=[x, w, x_zero_point], outputs=[y_with_padding],
name='test_convinteger_with_padding')
```
### ConvTranspose
There are 6 test cases, listed as following:
convtranspose
```python
x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)
[3., 4., 5.],
[6., 7., 8.]]]]).astype(np.float32)
W = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]]]).astype(np.float32)
node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"])
y = np.array([[[[0., 1., 3., 3., 2.], # (1, 2, 5, 5)
[3., 8., 15., 12., 7.],
[9., 21., 36., 27., 15.],
[9., 20., 33., 24., 13.],
[6., 13., 21., 15., 8.]],
[[0., 1., 3., 3., 2.],
[3., 8., 15., 12., 7.],
[9., 21., 36., 27., 15.],
[9., 20., 33., 24., 13.],
[6., 13., 21., 15., 8.]]]]).astype(np.float32)
expect(node, inputs=[x, W], outputs=[y], name='test_convtranspose')
```
convtranspose_1d
```python
x = np.array([[[0., 1., 2.]]]).astype(np.float32) # (1, 1, 3)
W = np.array([[[1., 1., 1.], # (1, 2, 3)
[1., 1., 1.]]]).astype(np.float32)
node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"])
y = np.array([[[0., 1., 3., 3., 2.], # (1, 2, 5)
[0., 1., 3., 3., 2.]]]).astype(np.float32)
expect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_1d')
```
convtranspose_3d
```python
x = np.array([[[[[0., 1., 2., 3., 4.], # (1, 1, 3, 4, 5)
[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.]]]]]).astype(np.float32)
W = np.array([[[[[1., 1., 1.], # (1, 2, 3, 3, 3)
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]],
[[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]]]]).astype(np.float32)
node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"])
y = np.array([[[[[0., 1., 3., 6., 9., 7., 4.], # (1, 2, 5, 6, 7)
[5., 12., 21., 27., 33., 24., 13.],
[15., 33., 54., 63., 72., 51., 27.],
[30., 63., 99., 108., 117., 81., 42.],
[25., 52., 81., 87., 93., 64., 33.],
[15., 31., 48., 51., 54., 37., 19.]],
[[20., 42., 66., 72., 78., 54., 28.],
[50., 104., 162., 174., 186., 128., 66.],
[90., 186., 288., 306., 324., 222., 114.],
[120., 246., 378., 396., 414., 282., 144.],
[90., 184., 282., 294., 306., 208., 106.],
[50., 102., 156., 162., 168., 114., 58.]],
[[60., 123., 189., 198., 207., 141., 72.],
[135., 276., 423., 441., 459., 312., 159.],
[225., 459., 702., 729., 756., 513., 261.],
[270., 549., 837., 864., 891., 603., 306.],
[195., 396., 603., 621., 639., 432., 219.],
[105., 213., 324., 333., 342., 231., 117.]],
[[60., 122., 186., 192., 198., 134., 68.],
[130., 264., 402., 414., 426., 288., 146.],
[210., 426., 648., 666., 684., 462., 234.],
[240., 486., 738., 756., 774., 522., 264.],
[170., 344., 522., 534., 546., 368., 186.],
[90., 182., 276., 282., 288., 194., 98.]],
[[40., 81., 123., 126., 129., 87., 44.],
[85., 172., 261., 267., 273., 184., 93.],
[135., 273., 414., 423., 432., 291., 147.],
[150., 303., 459., 468., 477., 321., 162.],
[105., 212., 321., 327., 333., 224., 113.],
[55., 111., 168., 171., 174., 117., 59.]]],
[[[0., 1., 3., 6., 9., 7., 4.],
[5., 12., 21., 27., 33., 24., 13.],
[15., 33., 54., 63., 72., 51., 27.],
[30., 63., 99., 108., 117., 81., 42.],
[25., 52., 81., 87., 93., 64., 33.],
[15., 31., 48., 51., 54., 37., 19.]],
[[20., 42., 66., 72., 78., 54., 28.],
[50., 104., 162., 174., 186., 128., 66.],
[90., 186., 288., 306., 324., 222., 114.],
[120., 246., 378., 396., 414., 282., 144.],
[90., 184., 282., 294., 306., 208., 106.],
[50., 102., 156., 162., 168., 114., 58.]],
[[60., 123., 189., 198., 207., 141., 72.],
[135., 276., 423., 441., 459., 312., 159.],
[225., 459., 702., 729., 756., 513., 261.],
[270., 549., 837., 864., 891., 603., 306.],
[195., 396., 603., 621., 639., 432., 219.],
[105., 213., 324., 333., 342., 231., 117.]],
[[60., 122., 186., 192., 198., 134., 68.],
[130., 264., 402., 414., 426., 288., 146.],
[210., 426., 648., 666., 684., 462., 234.],
[240., 486., 738., 756., 774., 522., 264.],
[170., 344., 522., 534., 546., 368., 186.],
[90., 182., 276., 282., 288., 194., 98.]],
[[40., 81., 123., 126., 129., 87., 44.],
[85., 172., 261., 267., 273., 184., 93.],
[135., 273., 414., 423., 432., 291., 147.],
[150., 303., 459., 468., 477., 321., 162.],
[105., 212., 321., 327., 333., 224., 113.],
[55., 111., 168., 171., 174., 117., 59.]]]]]).astype(np.float32)
expect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_3d')
```
convtranspose_attributes
```python
x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)
[3., 4., 5.],
[6., 7., 8.]]]]).astype(np.float32)
W = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]]]).astype(np.float32)
y = np.array([[[[0., 0., 1., 1., 3., 2., 2., 0.], # (1, 2, 10, 8)
[0., 0., 1., 1., 3., 2., 2., 0.],
[0., 0., 1., 1., 3., 2., 2., 0.],
[3., 3., 7., 4., 9., 5., 5., 0.],
[3., 3., 7., 4., 9., 5., 5., 0.],
[3., 3., 7., 4., 9., 5., 5., 0.],
[6., 6., 13., 7., 15., 8., 8., 0.],
[6., 6., 13., 7., 15., 8., 8., 0.],
[6., 6., 13., 7., 15., 8., 8., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]],
[[0., 0., 1., 1., 3., 2., 2., 0.],
[0., 0., 1., 1., 3., 2., 2., 0.],
[0., 0., 1., 1., 3., 2., 2., 0.],
[3., 3., 7., 4., 9., 5., 5., 0.],
[3., 3., 7., 4., 9., 5., 5., 0.],
[3., 3., 7., 4., 9., 5., 5., 0.],
[6., 6., 13., 7., 15., 8., 8., 0.],
[6., 6., 13., 7., 15., 8., 8., 0.],
[6., 6., 13., 7., 15., 8., 8., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]]]).astype(np.float32)
node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"],
strides=[3, 2],
output_shape=[10, 8])
expect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_output_shape')
node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"],
strides=[3, 2],
output_padding=[1, 1])
expect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_pad')
node = onnx.helper.make_node(
'ConvTranspose', ['X', 'W'], ['Y'],
name='test',
strides=[3, 2],
output_shape=[10, 8],
kernel_shape=[3, 3],
output_padding=[1, 1]
)
expect(node, inputs=[x, W], outputs=[y],
name='test_convtranspose_kernel_shape')
```
convtranspose_dilations
```python
x = np.array([[[[3., 8., 1.], # (1, 1, 3, 3)
[9., 5., 7.],
[3., 2., 6.]]]]).astype(np.float32)
W = np.array([[[[7., 2.], # (1, 1, 2, 2)
[1., 9.]]]]).astype(np.float32)
node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"], dilations=[2, 2])
y = np.array([[[[21., 56., 13., 16., 2.], # [1, 1, 5, 5]
[63., 35., 67., 10., 14.],
[24., 22., 76., 76., 21.],
[9., 5., 88., 45., 63.],
[3., 2., 33., 18., 54.]]]]).astype(np.float32)
expect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_dilations')
```
convtranspose_pads
```python
x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)
[3., 4., 5.],
[6., 7., 8.]]]]).astype(np.float32)
W = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]]]).astype(np.float32)
node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"],
strides=[3, 2],
pads=[1, 2, 1, 2])
y = np.array([[[[1., 1., 3.], # (1, 2, 7, 3)
[1., 1., 3.],
[7., 4., 9.],
[7., 4., 9.],
[7., 4., 9.],
[13., 7., 15.],
[13., 7., 15.]],
[[1., 1., 3.],
[1., 1., 3.],
[7., 4., 9.],
[7., 4., 9.],
[7., 4., 9.],
[13., 7., 15.],
[13., 7., 15.]]]]).astype(np.float32)
expect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_pads')
```
### Cos
There are 1 test cases, listed as following:
cos
```python
node = onnx.helper.make_node(
'Cos',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.cos(x)
expect(node, inputs=[x], outputs=[y],
name='test_cos_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.cos(x)
expect(node, inputs=[x], outputs=[y],
name='test_cos')
```
### Cosh
There are 1 test cases, listed as following:
cosh
```python
node = onnx.helper.make_node(
'Cosh',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.cosh(x) # expected output [1.54308069, 1., 1.54308069]
expect(node, inputs=[x], outputs=[y],
name='test_cosh_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.cosh(x)
expect(node, inputs=[x], outputs=[y],
name='test_cosh')
```
### CumSum
There are 7 test cases, listed as following:
cumsum_1d
```python
node = onnx.helper.make_node(
'CumSum',
inputs=['x', 'axis'],
outputs=['y']
)
x = np.array([1., 2., 3., 4., 5.]).astype(np.float64)
axis = np.array([0]).astype(np.int32)
y = np.array([1., 3., 6., 10., 15.]).astype(np.float64)
expect(node, inputs=[x, axis], outputs=[y],
name='test_cumsum_1d')
```
cumsum_1d_exclusive
```python
node = onnx.helper.make_node(
'CumSum',
inputs=['x', 'axis'],
outputs=['y'],
exclusive=1
)
x = np.array([1., 2., 3., 4., 5.]).astype(np.float64)
axis = np.array([0]).astype(np.int32)
y = np.array([0., 1., 3., 6., 10.]).astype(np.float64)
expect(node, inputs=[x, axis], outputs=[y],
name='test_cumsum_1d_exclusive')
```
cumsum_1d_reverse
```python
node = onnx.helper.make_node(
'CumSum',
inputs=['x', 'axis'],
outputs=['y'],
reverse=1
)
x = np.array([1., 2., 3., 4., 5.]).astype(np.float64)
axis = np.array([0]).astype(np.int32)
y = np.array([15., 14., 12., 9., 5.]).astype(np.float64)
expect(node, inputs=[x, axis], outputs=[y],
name='test_cumsum_1d_reverse')
```
cumsum_1d_reverse_exclusive
```python
node = onnx.helper.make_node(
'CumSum',
inputs=['x', 'axis'],
outputs=['y'],
reverse=1
)
x = np.array([1., 2., 3., 4., 5.]).astype(np.float64)
axis = np.array([0]).astype(np.int32)
y = np.array([14., 12., 9., 5., 0.]).astype(np.float64)
expect(node, inputs=[x, axis], outputs=[y],
name='test_cumsum_1d_reverse_exclusive')
```
cumsum_2d_axis_0
```python
node = onnx.helper.make_node(
'CumSum',
inputs=['x', 'axis'],
outputs=['y'],
)
x = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))
axis = np.array([0]).astype(np.int32)
y = np.array([1., 2., 3., 5., 7., 9.]).astype(np.float64).reshape((2, 3))
expect(node, inputs=[x, axis], outputs=[y],
name='test_cumsum_2d_axis_0')
```
cumsum_2d_axis_1
```python
node = onnx.helper.make_node(
'CumSum',
inputs=['x', 'axis'],
outputs=['y'],
)
x = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))
axis = np.array([1]).astype(np.int32)
y = np.array([1., 3., 6., 4., 9., 15.]).astype(np.float64).reshape((2, 3))
expect(node, inputs=[x, axis], outputs=[y],
name='test_cumsum_2d_axis_1')
```
cumsum_2d_negative_axis
```python
node = onnx.helper.make_node(
'CumSum',
inputs=['x', 'axis'],
outputs=['y'],
)
x = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))
axis = np.array([-1]).astype(np.int32)
y = np.array([1., 3., 6., 4., 9., 15.]).astype(np.float64).reshape((2, 3))
expect(node, inputs=[x, axis], outputs=[y],
name='test_cumsum_2d_negative_axis')
```
### DepthToSpace
There are 2 test cases, listed as following:
crd_mode_example
```python
node = onnx.helper.make_node(
'DepthToSpace',
inputs=['x'],
outputs=['y'],
blocksize=2,
mode='CRD'
)
# (1, 8, 2, 3) input tensor
x = np.array([[[[0., 1., 2.],
[3., 4., 5.]],
[[9., 10., 11.],
[12., 13., 14.]],
[[18., 19., 20.],
[21., 22., 23.]],
[[27., 28., 29.],
[30., 31., 32.]],
[[36., 37., 38.],
[39., 40., 41.]],
[[45., 46., 47.],
[48., 49., 50.]],
[[54., 55., 56.],
[57., 58., 59.]],
[[63., 64., 65.],
[66., 67., 68.]]]]).astype(np.float32)
# (1, 2, 4, 6) output tensor
y = np.array([[[[0., 9., 1., 10., 2., 11.],
[18., 27., 19., 28., 20., 29.],
[3., 12., 4., 13., 5., 14.],
[21., 30., 22., 31., 23., 32.]],
[[36., 45., 37., 46., 38., 47.],
[54., 63., 55., 64., 56., 65.],
[39., 48., 40., 49., 41., 50.],
[57., 66., 58., 67., 59., 68.]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y],
name='test_depthtospace_crd_mode_example')
```
default_mode_example
```python
node = onnx.helper.make_node(
'DepthToSpace',
inputs=['x'],
outputs=['y'],
blocksize=2,
mode='DCR'
)
# (1, 8, 2, 3) input tensor
x = np.array([[[[0., 1., 2.],
[3., 4., 5.]],
[[9., 10., 11.],
[12., 13., 14.]],
[[18., 19., 20.],
[21., 22., 23.]],
[[27., 28., 29.],
[30., 31., 32.]],
[[36., 37., 38.],
[39., 40., 41.]],
[[45., 46., 47.],
[48., 49., 50.]],
[[54., 55., 56.],
[57., 58., 59.]],
[[63., 64., 65.],
[66., 67., 68.]]]]).astype(np.float32)
# (1, 2, 4, 6) output tensor
y = np.array([[[[0., 18., 1., 19., 2., 20.],
[36., 54., 37., 55., 38., 56.],
[3., 21., 4., 22., 5., 23.],
[39., 57., 40., 58., 41., 59.]],
[[9., 27., 10., 28., 11., 29.],
[45., 63., 46., 64., 47., 65.],
[12., 30., 13., 31., 14., 32.],
[48., 66., 49., 67., 50., 68.]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y],
name='test_depthtospace_example')
```
### DequantizeLinear
There are 1 test cases, listed as following:
dequantizelinear
```python
node = onnx.helper.make_node('DequantizeLinear',
inputs=['x', 'x_scale', 'x_zero_point'],
outputs=['y'],)
# scalar zero point and scale
x = np.array([0, 3, 128, 255]).astype(np.uint8)
x_scale = np.float32(2)
x_zero_point = np.uint8(128)
y = np.array([-256, -250, 0, 254], dtype=np.float32)
expect(node, inputs=[x, x_scale, x_zero_point], outputs=[y],
name='test_dequantizelinear')
```
### Det
There are 2 test cases, listed as following:
2d
```python
node = onnx.helper.make_node(
'Det',
inputs=['x'],
outputs=['y'],
)
x = np.arange(4).reshape(2, 2).astype(np.float32)
y = np.linalg.det(x) # expect -2
expect(node, inputs=[x], outputs=[y],
name='test_det_2d')
```
nd
```python
node = onnx.helper.make_node(
'Det',
inputs=['x'],
outputs=['y'],
)
x = np.array([[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]]).astype(np.float32)
y = np.linalg.det(x) # expect array([-2., -3., -8.])
expect(node, inputs=[x], outputs=[y],
name='test_det_nd')
```
### Div
There are 2 test cases, listed as following:
div
```python
node = onnx.helper.make_node(
'Div',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([3, 4]).astype(np.float32)
y = np.array([1, 2]).astype(np.float32)
z = x / y # expected output [3., 2.]
expect(node, inputs=[x, y], outputs=[z],
name='test_div_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.rand(3, 4, 5).astype(np.float32) + 1.0
z = x / y
expect(node, inputs=[x, y], outputs=[z],
name='test_div')
```
div_broadcast
```python
node = onnx.helper.make_node(
'Div',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.rand(5).astype(np.float32) + 1.0
z = x / y
expect(node, inputs=[x, y], outputs=[z],
name='test_div_bcast')
```
### Dropout
There are 2 test cases, listed as following:
default
```python
node = onnx.helper.make_node(
'Dropout',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = x
expect(node, inputs=[x], outputs=[y],
name='test_dropout_default')
```
random
```python
node = onnx.helper.make_node(
'Dropout',
inputs=['x'],
outputs=['y'],
ratio=.2,
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = x
expect(node, inputs=[x], outputs=[y],
name='test_dropout_random')
```
### DynamicQuantizeLinear
There are 1 test cases, listed as following:
dynamicquantizelinear
```python
node = onnx.helper.make_node('DynamicQuantizeLinear',
inputs=['x'],
outputs=['y', 'y_scale', 'y_zero_point'],
)
# expected scale 0.0196078438 and zero point 153
X = np.array([0, 2, -3, -2.5, 1.34, 0.5]).astype(np.float32)
x_min = np.minimum(0, np.min(X))
x_max = np.maximum(0, np.max(X))
Y_Scale = np.float32((x_max - x_min) / (255 - 0)) # uint8 -> [0, 255]
Y_ZeroPoint = np.clip(round((0 - x_min) / Y_Scale), 0, 255).astype(np.uint8)
Y = np.clip(np.round(X / Y_Scale) + Y_ZeroPoint, 0, 255).astype(np.uint8)
expect(node, inputs=[X], outputs=[Y, Y_Scale, Y_ZeroPoint],
name='test_dynamicquantizelinear')
# expected scale 0.0156862754 and zero point 255
X = np.array([-1.0, -2.1, -1.3, -2.5, -3.34, -4.0]).astype(np.float32)
x_min = np.minimum(0, np.min(X))
x_max = np.maximum(0, np.max(X))
Y_Scale = np.float32((x_max - x_min) / (255 - 0)) # uint8 -> [0, 255]
Y_ZeroPoint = np.clip(round((0 - x_min) / Y_Scale), 0, 255).astype(np.uint8)
Y = np.clip(np.round(X / Y_Scale) + Y_ZeroPoint, 0, 255).astype(np.uint8)
expect(node, inputs=[X], outputs=[Y, Y_Scale, Y_ZeroPoint],
name='test_dynamicquantizelinear_max_adjusted')
X = np.array([1, 2.1, 1.3, 2.5,
3.34, 4.0, 1.5, 2.6,
3.9, 4.0, 3.0, 2.345]).astype(np.float32).reshape((3, 4))
# expected scale 0.0156862754 and zero point 0
x_min = np.minimum(0, np.min(X))
x_max = np.maximum(0, np.max(X))
Y_Scale = np.float32((x_max - x_min) / (255 - 0)) # uint8 -> [0, 255]
Y_ZeroPoint = np.clip(round((0 - x_min) / Y_Scale), 0, 255).astype(np.uint8)
Y = np.clip(np.round(X / Y_Scale) + Y_ZeroPoint, 0, 255).astype(np.uint8)
expect(node, inputs=[X], outputs=[Y, Y_Scale, Y_ZeroPoint],
name='test_dynamicquantizelinear_min_adjusted')
```
### Elu
There are 2 test cases, listed as following:
elu
```python
node = onnx.helper.make_node(
'Elu',
inputs=['x'],
outputs=['y'],
alpha=2.0
)
x = np.array([-1, 0, 1]).astype(np.float32)
# expected output [-1.2642411, 0., 1.]
y = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0
expect(node, inputs=[x], outputs=[y],
name='test_elu_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0
expect(node, inputs=[x], outputs=[y],
name='test_elu')
```
elu_default
```python
default_alpha = 1.0
node = onnx.helper.make_node(
'Elu',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha
expect(node, inputs=[x], outputs=[y],
name='test_elu_default')
```
### Equal
There are 2 test cases, listed as following:
equal
```python
node = onnx.helper.make_node(
'Equal',
inputs=['x', 'y'],
outputs=['z'],
)
x = (np.random.randn(3, 4, 5) * 10).astype(np.int32)
y = (np.random.randn(3, 4, 5) * 10).astype(np.int32)
z = np.equal(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_equal')
```
equal_broadcast
```python
node = onnx.helper.make_node(
'Equal',
inputs=['x', 'y'],
outputs=['z'],
)
x = (np.random.randn(3, 4, 5) * 10).astype(np.int32)
y = (np.random.randn(5) * 10).astype(np.int32)
z = np.equal(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_equal_bcast')
```
### Erf
There are 1 test cases, listed as following:
erf
```python
node = onnx.helper.make_node(
'Erf',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
y = np.vectorize(math.erf)(x).astype(np.float32)
expect(node, inputs=[x], outputs=[y],
name='test_erf')
```
### Exp
There are 1 test cases, listed as following:
exp
```python
node = onnx.helper.make_node(
'Exp',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.exp(x) # expected output [0.36787945, 1., 2.71828175]
expect(node, inputs=[x], outputs=[y],
name='test_exp_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.exp(x)
expect(node, inputs=[x], outputs=[y],
name='test_exp')
```
### Expand
There are 2 test cases, listed as following:
dim_changed
```python
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
```python
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')
```
### EyeLike
There are 3 test cases, listed as following:
populate_off_main_diagonal
```python
shape = (4, 5)
off_diagonal_offset = 1
node = onnx.helper.make_node(
'EyeLike',
inputs=['x'],
outputs=['y'],
k=off_diagonal_offset,
dtype=onnx.TensorProto.FLOAT,
)
x = np.random.randint(0, 100, size=shape, dtype=np.int32)
y = np.eye(shape[0], shape[1], k=off_diagonal_offset, dtype=np.float32)
expect(node, inputs=[x], outputs=[y], name='test_eyelike_populate_off_main_diagonal')
```
with_dtype
```python
shape = (3, 4)
node = onnx.helper.make_node(
'EyeLike',
inputs=['x'],
outputs=['y'],
dtype=onnx.TensorProto.DOUBLE,
)
x = np.random.randint(0, 100, size=shape, dtype=np.int32)
y = np.eye(shape[0], shape[1], dtype=np.float64)
expect(node, inputs=[x], outputs=[y], name='test_eyelike_with_dtype')
```
without_dtype
```python
shape = (4, 4)
node = onnx.helper.make_node(
'EyeLike',
inputs=['x'],
outputs=['y'],
)
x = np.random.randint(0, 100, size=shape, dtype=np.int32)
y = np.eye(shape[0], shape[1], dtype=np.int32)
expect(node, inputs=[x], outputs=[y], name='test_eyelike_without_dtype')
```
### Flatten
There are 3 test cases, listed as following:
flatten
```python
shape = (2, 3, 4, 5)
a = np.random.random_sample(shape).astype(np.float32)
for i in range(len(shape)):
node = onnx.helper.make_node(
'Flatten',
inputs=['a'],
outputs=['b'],
axis=i,
)
new_shape = (1, -1) if i == 0 else (np.prod(shape[0:i]).astype(int), -1)
b = np.reshape(a, new_shape)
expect(node, inputs=[a], outputs=[b],
name='test_flatten_axis' + str(i))
```
flatten_negative_axis
```python
shape = (2, 3, 4, 5)
a = np.random.random_sample(shape).astype(np.float32)
for i in range(-len(shape), 0):
node = onnx.helper.make_node(
'Flatten',
inputs=['a'],
outputs=['b'],
axis=i,
)
new_shape = (np.prod(shape[0:i]).astype(int), -1)
b = np.reshape(a, new_shape)
expect(node, inputs=[a], outputs=[b],
name='test_flatten_negative_axis' + str(abs(i)))
```
flatten_with_default_axis
```python
node = onnx.helper.make_node(
'Flatten',
inputs=['a'],
outputs=['b'], # Default value for axis: axis=1
)
shape = (5, 4, 3, 2)
a = np.random.random_sample(shape).astype(np.float32)
new_shape = (5, 24)
b = np.reshape(a, new_shape)
expect(node, inputs=[a], outputs=[b],
name='test_flatten_default_axis')
```
### Floor
There are 1 test cases, listed as following:
floor
```python
node = onnx.helper.make_node(
'Floor',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1.5, 1.2, 2]).astype(np.float32)
y = np.floor(x) # expected output [-2., 1., 2.]
expect(node, inputs=[x], outputs=[y],
name='test_floor_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.floor(x)
expect(node, inputs=[x], outputs=[y],
name='test_floor')
```
### GRU
There are 3 test cases, listed as following:
defaults
```python
input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)
input_size = 2
hidden_size = 5
weight_scale = 0.1
number_of_gates = 3
node = onnx.helper.make_node(
'GRU',
inputs=['X', 'W', 'R'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
W = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
gru = GRU_Helper(X=input, W=W, R=R)
_, Y_h = gru.step()
expect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_gru_defaults')
```
initial_bias
```python
input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)
input_size = 3
hidden_size = 3
weight_scale = 0.1
custom_bias = 0.1
number_of_gates = 3
node = onnx.helper.make_node(
'GRU',
inputs=['X', 'W', 'R', 'B'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
W = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
# Adding custom bias
W_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)
R_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)
B = np.concatenate((W_B, R_B), axis=1)
gru = GRU_Helper(X=input, W=W, R=R, B=B)
_, Y_h = gru.step()
expect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_with_initial_bias')
```
seq_length
```python
input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
[[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)
input_size = 3
hidden_size = 5
number_of_gates = 3
node = onnx.helper.make_node(
'GRU',
inputs=['X', 'W', 'R', 'B'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
W = np.random.randn(1, number_of_gates * hidden_size, input_size).astype(np.float32)
R = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(np.float32)
# Adding custom bias
W_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)
R_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)
B = np.concatenate((W_B, R_B), axis=1)
gru = GRU_Helper(X=input, W=W, R=R, B=B)
_, Y_h = gru.step()
expect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_seq_length')
```
### Gather
There are 3 test cases, listed as following:
gather_0
```python
node = onnx.helper.make_node(
'Gather',
inputs=['data', 'indices'],
outputs=['y'],
axis=0,
)
data = np.random.randn(5, 4, 3, 2).astype(np.float32)
indices = np.array([0, 1, 3])
y = np.take(data, indices, axis=0)
expect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],
name='test_gather_0')
```
gather_1
```python
node = onnx.helper.make_node(
'Gather',
inputs=['data', 'indices'],
outputs=['y'],
axis=1,
)
data = np.random.randn(5, 4, 3, 2).astype(np.float32)
indices = np.array([0, 1, 3])
y = np.take(data, indices, axis=1)
expect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],
name='test_gather_1')
```
gather_negative_indices
```python
node = onnx.helper.make_node(
'Gather',
inputs=['data', 'indices'],
outputs=['y'],
axis=0,
)
data = np.arange(10).astype(np.float32)
indices = np.array([0, -9, -10])
y = np.take(data, indices, axis=0)
expect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],
name='test_gather_negative_indices')
```
### GatherElements
There are 3 test cases, listed as following:
gather_elements_0
```python
axis = 1
node = onnx.helper.make_node(
'GatherElements',
inputs=['data', 'indices'],
outputs=['y'],
axis=axis,
)
data = np.array([[1, 2],
[3, 4]], dtype=np.float32)
indices = np.array([[0, 0],
[1, 0]], dtype=np.int32)
y = gather_elements(data, indices, axis)
# print(y) produces
# [[1, 1],
# [4, 3]]
expect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],
name='test_gather_elements_0')
```
gather_elements_1
```python
axis = 0
node = onnx.helper.make_node(
'GatherElements',
inputs=['data', 'indices'],
outputs=['y'],
axis=axis,
)
data = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=np.float32)
indices = np.array([[1, 2, 0],
[2, 0, 0]], dtype=np.int32)
y = gather_elements(data, indices, axis)
# print(y) produces
# [[4, 8, 3],
# [7, 2, 3]]
expect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],
name='test_gather_elements_1')
```
gather_elements_negative_indices
```python
axis = 0
node = onnx.helper.make_node(
'GatherElements',
inputs=['data', 'indices'],
outputs=['y'],
axis=axis,
)
data = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=np.float32)
indices = np.array([[-1, -2, 0],
[-2, 0, 0]], dtype=np.int32)
y = gather_elements(data, indices, axis)
# print(y) produces
# [[7, 5, 3],
# [4, 2, 3]]
expect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],
name='test_gather_elements_negative_indices')
```
### GatherND
There are 2 test cases, listed as following:
float32
```python
node = onnx.helper.make_node(
'GatherND',
inputs=['data', 'indices'],
outputs=['output'],
)
data = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype=np.float32)
indices = np.array([[[0, 1]], [[1, 0]]], dtype=np.int64)
output = gather_nd_impl(data, indices)
expected_output = np.array([[[2, 3]], [[4, 5]]], dtype=np.float32)
assert (np.array_equal(output, expected_output))
expect(node, inputs=[data, indices], outputs=[output],
name='test_gathernd_example_float32')
```
int32
```python
node = onnx.helper.make_node(
'GatherND',
inputs=['data', 'indices'],
outputs=['output'],
)
data = np.array([[0, 1], [2, 3]], dtype=np.int32)
indices = np.array([[0, 0], [1, 1]], dtype=np.int64)
output = gather_nd_impl(data, indices)
expected_output = np.array([0, 3], dtype=np.int32)
assert (np.array_equal(output, expected_output))
expect(node, inputs=[data, indices], outputs=[output],
name='test_gathernd_example_int32')
```
### Gemm
There are 11 test cases, listed as following:
all_attributes
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y'],
alpha=0.25,
beta=0.35,
transA=1,
transB=1
)
a = np.random.ranf([4, 3]).astype(np.float32)
b = np.random.ranf([5, 4]).astype(np.float32)
c = np.random.ranf([1, 5]).astype(np.float32)
y = gemm_reference_implementation(a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_all_attributes')
```
alpha
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y'],
alpha=0.5
)
a = np.random.ranf([3, 5]).astype(np.float32)
b = np.random.ranf([5, 4]).astype(np.float32)
c = np.zeros([1, 4]).astype(np.float32)
y = gemm_reference_implementation(a, b, c, alpha=0.5)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_alpha')
```
beta
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y'],
beta=0.5
)
a = np.random.ranf([2, 7]).astype(np.float32)
b = np.random.ranf([7, 4]).astype(np.float32)
c = np.random.ranf([1, 4]).astype(np.float32)
y = gemm_reference_implementation(a, b, c, beta=0.5)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_beta')
```
default_matrix_bias
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y']
)
a = np.random.ranf([3, 6]).astype(np.float32)
b = np.random.ranf([6, 4]).astype(np.float32)
c = np.random.ranf([3, 4]).astype(np.float32)
y = gemm_reference_implementation(a, b, c)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_default_matrix_bias')
```
default_no_bias
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b'],
outputs=['y']
)
a = np.random.ranf([2, 10]).astype(np.float32)
b = np.random.ranf([10, 3]).astype(np.float32)
y = gemm_reference_implementation(a, b)
expect(node, inputs=[a, b], outputs=[y],
name='test_gemm_default_no_bias')
```
default_scalar_bias
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y']
)
a = np.random.ranf([2, 3]).astype(np.float32)
b = np.random.ranf([3, 4]).astype(np.float32)
c = np.array(3.14).astype(np.float32)
y = gemm_reference_implementation(a, b, c)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_default_scalar_bias')
```
default_single_elem_vector_bias
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y']
)
a = np.random.ranf([3, 7]).astype(np.float32)
b = np.random.ranf([7, 3]).astype(np.float32)
c = np.random.ranf([1]).astype(np.float32)
y = gemm_reference_implementation(a, b, c)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_default_single_elem_vector_bias')
```
default_vector_bias
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y']
)
a = np.random.ranf([2, 7]).astype(np.float32)
b = np.random.ranf([7, 4]).astype(np.float32)
c = np.random.ranf([1, 4]).astype(np.float32)
y = gemm_reference_implementation(a, b, c)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_default_vector_bias')
```
default_zero_bias
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y']
)
a = np.random.ranf([3, 5]).astype(np.float32)
b = np.random.ranf([5, 4]).astype(np.float32)
c = np.zeros([1, 4]).astype(np.float32)
y = gemm_reference_implementation(a, b, c)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_default_zero_bias')
```
transposeA
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y'],
transA=1
)
a = np.random.ranf([6, 3]).astype(np.float32)
b = np.random.ranf([6, 4]).astype(np.float32)
c = np.zeros([1, 4]).astype(np.float32)
y = gemm_reference_implementation(a, b, c, transA=1)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_transposeA')
```
transposeB
```python
node = onnx.helper.make_node(
'Gemm',
inputs=['a', 'b', 'c'],
outputs=['y'],
transB=1
)
a = np.random.ranf([3, 6]).astype(np.float32)
b = np.random.ranf([4, 6]).astype(np.float32)
c = np.zeros([1, 4]).astype(np.float32)
y = gemm_reference_implementation(a, b, c, transB=1)
expect(node, inputs=[a, b, c], outputs=[y],
name='test_gemm_transposeB')
```
### GlobalAveragePool
There are 2 test cases, listed as following:
globalaveragepool
```python
node = onnx.helper.make_node(
'GlobalAveragePool',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(1, 3, 5, 5).astype(np.float32)
spatial_shape = np.ndim(x) - 2
y = np.average(x, axis=tuple(range(spatial_shape, spatial_shape + 2)))
for _ in range(spatial_shape):
y = np.expand_dims(y, -1)
expect(node, inputs=[x], outputs=[y], name='test_globalaveragepool')
```
globalaveragepool_precomputed
```python
node = onnx.helper.make_node(
'GlobalAveragePool',
inputs=['x'],
outputs=['y'],
)
x = np.array([[[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]]]).astype(np.float32)
y = np.array([[[[5]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_globalaveragepool_precomputed')
```
### GlobalMaxPool
There are 2 test cases, listed as following:
globalmaxpool
```python
node = onnx.helper.make_node(
'GlobalMaxPool',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(1, 3, 5, 5).astype(np.float32)
spatial_shape = np.ndim(x) - 2
y = np.max(x, axis=tuple(range(spatial_shape, spatial_shape + 2)))
for _ in range(spatial_shape):
y = np.expand_dims(y, -1)
expect(node, inputs=[x], outputs=[y], name='test_globalmaxpool')
```
globalmaxpool_precomputed
```python
node = onnx.helper.make_node(
'GlobalMaxPool',
inputs=['x'],
outputs=['y'],
)
x = np.array([[[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]]]).astype(np.float32)
y = np.array([[[[9]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_globalmaxpool_precomputed')
```
### Greater
There are 2 test cases, listed as following:
greater
```python
node = onnx.helper.make_node(
'Greater',
inputs=['x', 'y'],
outputs=['greater'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(3, 4, 5).astype(np.float32)
z = np.greater(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_greater')
```
greater_broadcast
```python
node = onnx.helper.make_node(
'Greater',
inputs=['x', 'y'],
outputs=['greater'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(5).astype(np.float32)
z = np.greater(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_greater_bcast')
```
### HardSigmoid
There are 2 test cases, listed as following:
hardsigmoid
```python
node = onnx.helper.make_node(
'HardSigmoid',
inputs=['x'],
outputs=['y'],
alpha=0.5,
beta=0.6
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.clip(x * 0.5 + 0.6, 0, 1) # expected output [0.1, 0.6, 1.]
expect(node, inputs=[x], outputs=[y],
name='test_hardsigmoid_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x * 0.5 + 0.6, 0, 1)
expect(node, inputs=[x], outputs=[y],
name='test_hardsigmoid')
```
hardsigmoid_default
```python
default_alpha = 0.2
default_beta = 0.5
node = onnx.helper.make_node(
'HardSigmoid',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x * default_alpha + default_beta, 0, 1)
expect(node, inputs=[x], outputs=[y],
name='test_hardsigmoid_default')
```
### Hardmax
There are 2 test cases, listed as following:
hardmax
```python
node = onnx.helper.make_node(
'Hardmax',
inputs=['x'],
outputs=['y'],
)
x = np.array([[3, 0, 1, 2], [2, 5, 1, 0], [0, 1, 3, 2], [0, 1, 2, 3]]).astype(np.float32)
y = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y],
name='test_hardmax_example')
# For multiple occurrances of the maximal values, the first occurrence is selected for one-hot output
x = np.array([[3, 3, 3, 1]]).astype(np.float32)
y = np.array([[1, 0, 0, 0]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y],
name='test_hardmax_one_hot')
```
hardmax_axis
```python
def hardmax_2d(x): # type: (np.ndarray) -> np.ndarray
return np.eye(x.shape[1], dtype=x.dtype)[np.argmax(x, axis=1)]
x = np.random.randn(3, 4, 5).astype(np.float32)
node = onnx.helper.make_node(
'Hardmax',
inputs=['x'],
outputs=['y'],
axis=0,
)
y = hardmax_2d(x.reshape(1, 60)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_hardmax_axis_0')
node = onnx.helper.make_node(
'Hardmax',
inputs=['x'],
outputs=['y'],
axis=1,
)
y = hardmax_2d(x.reshape(3, 20)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_hardmax_axis_1')
# default axis is 1
node = onnx.helper.make_node(
'Hardmax',
inputs=['x'],
outputs=['y'],
)
expect(node, inputs=[x], outputs=[y],
name='test_hardmax_default_axis')
node = onnx.helper.make_node(
'Hardmax',
inputs=['x'],
outputs=['y'],
axis=2,
)
y = hardmax_2d(x.reshape(12, 5)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_hardmax_axis_2')
node = onnx.helper.make_node(
'Hardmax',
inputs=['x'],
outputs=['y'],
axis=-1,
)
y = hardmax_2d(x.reshape(12, 5)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_hardmax_negative_axis')
```
### Identity
There are 1 test cases, listed as following:
identity
```python
node = onnx.helper.make_node(
'Identity',
inputs=['x'],
outputs=['y'],
)
data = np.array([[[
[1, 2],
[3, 4],
]]], dtype=np.float32)
expect(node, inputs=[data], outputs=[data],
name='test_identity')
```
### InstanceNormalization
There are 1 test cases, listed as following:
instancenormalization
```python
def _instancenorm_test_mode(x, s, bias, epsilon=1e-5): # type: ignore
dims_x = len(x.shape)
axis = tuple(range(2, dims_x))
mean = np.mean(x, axis=axis, keepdims=True)
var = np.var(x, axis=axis, keepdims=True)
dim_ones = (1,) * (dims_x - 2)
s = s.reshape(-1, *dim_ones)
bias = bias.reshape(-1, *dim_ones)
return s * (x - mean) / np.sqrt(var + epsilon) + bias
# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
y = _instancenorm_test_mode(x, s, bias).astype(np.float32)
node = onnx.helper.make_node(
'InstanceNormalization',
inputs=['x', 's', 'bias'],
outputs=['y'],
)
# output size: (1, 2, 1, 3)
expect(node, inputs=[x, s, bias], outputs=[y],
name='test_instancenorm_example')
# input size: (2, 3, 4, 5)
x = np.random.randn(2, 3, 4, 5).astype(np.float32)
s = np.random.randn(3).astype(np.float32)
bias = np.random.randn(3).astype(np.float32)
epsilon = 1e-2
y = _instancenorm_test_mode(x, s, bias, epsilon).astype(np.float32)
node = onnx.helper.make_node(
'InstanceNormalization',
inputs=['x', 's', 'bias'],
outputs=['y'],
epsilon=epsilon,
)
# output size: (2, 3, 4, 5)
expect(node, inputs=[x, s, bias], outputs=[y],
name='test_instancenorm_epsilon')
```
### IsInf
There are 3 test cases, listed as following:
infinity
```python
node = onnx.helper.make_node('IsInf',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1.2, np.nan, np.inf, 2.8, np.NINF, np.inf],
dtype=np.float32)
y = np.isinf(x)
expect(node, inputs=[x], outputs=[y], name='test_isinf')
```
negative_infinity_only
```python
node = onnx.helper.make_node('IsInf',
inputs=['x'],
outputs=['y'],
detect_positive=0
)
x = np.array([-1.7, np.nan, np.inf, -3.6, np.NINF, np.inf],
dtype=np.float32)
y = np.isneginf(x)
expect(node, inputs=[x], outputs=[y], name='test_isinf_negative')
```
positive_infinity_only
```python
node = onnx.helper.make_node('IsInf',
inputs=['x'],
outputs=['y'],
detect_negative=0
)
x = np.array([-1.7, np.nan, np.inf, 3.6, np.NINF, np.inf],
dtype=np.float32)
y = np.isposinf(x)
expect(node, inputs=[x], outputs=[y], name='test_isinf_positive')
```
### IsNaN
There are 1 test cases, listed as following:
isnan
```python
node = onnx.helper.make_node(
'IsNaN',
inputs=['x'],
outputs=['y'],
)
x = np.array([3.0, np.nan, 4.0, np.nan], dtype=np.float32)
y = np.isnan(x)
expect(node, inputs=[x], outputs=[y], name='test_isnan')
```
### LRN
There are 2 test cases, listed as following:
default
```python
alpha = 0.0001
beta = 0.75
bias = 1.0
nsize = 3
node = onnx.helper.make_node(
'LRN',
inputs=['x'],
outputs=['y'],
size=3
)
x = np.random.randn(5, 5, 5, 5).astype(np.float32)
square_sum = np.zeros((5, 5, 5, 5)).astype(np.float32)
for n, c, h, w in np.ndindex(x.shape):
square_sum[n, c, h, w] = sum(x[n,
max(0, c - int(math.floor((nsize - 1) / 2))):min(5, c + int(math.ceil((nsize - 1) / 2)) + 1),
h,
w] ** 2)
y = x / ((bias + (alpha / nsize) * square_sum) ** beta)
expect(node, inputs=[x], outputs=[y],
name='test_lrn_default')
```
lrn
```python
alpha = 0.0002
beta = 0.5
bias = 2.0
nsize = 3
node = onnx.helper.make_node(
'LRN',
inputs=['x'],
outputs=['y'],
alpha=alpha,
beta=beta,
bias=bias,
size=nsize
)
x = np.random.randn(5, 5, 5, 5).astype(np.float32)
square_sum = np.zeros((5, 5, 5, 5)).astype(np.float32)
for n, c, h, w in np.ndindex(x.shape):
square_sum[n, c, h, w] = sum(x[n,
max(0, c - int(math.floor((nsize - 1) / 2))):min(5, c + int(math.ceil((nsize - 1) / 2)) + 1),
h,
w] ** 2)
y = x / ((bias + (alpha / nsize) * square_sum) ** beta)
expect(node, inputs=[x], outputs=[y],
name='test_lrn')
```
### LSTM
There are 3 test cases, listed as following:
defaults
```python
input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)
input_size = 2
hidden_size = 3
weight_scale = 0.1
number_of_gates = 4
node = onnx.helper.make_node(
'LSTM',
inputs=['X', 'W', 'R'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
W = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
lstm = LSTM_Helper(X=input, W=W, R=R)
_, Y_h = lstm.step()
expect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_lstm_defaults')
```
initial_bias
```python
input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)
input_size = 3
hidden_size = 4
weight_scale = 0.1
custom_bias = 0.1
number_of_gates = 4
node = onnx.helper.make_node(
'LSTM',
inputs=['X', 'W', 'R', 'B'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
W = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
# Adding custom bias
W_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)
R_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)
B = np.concatenate((W_B, R_B), 1)
lstm = LSTM_Helper(X=input, W=W, R=R, B=B)
_, Y_h = lstm.step()
expect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_lstm_with_initial_bias')
```
peepholes
```python
input = np.array([[[1., 2., 3., 4.], [5., 6., 7., 8.]]]).astype(np.float32)
input_size = 4
hidden_size = 3
weight_scale = 0.1
number_of_gates = 4
number_of_peepholes = 3
node = onnx.helper.make_node(
'LSTM',
inputs=['X', 'W', 'R', 'B', 'sequence_lens', 'initial_h', 'initial_c', 'P'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
# Initializing Inputs
W = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
B = np.zeros((1, 2 * number_of_gates * hidden_size)).astype(np.float32)
seq_lens = np.repeat(input.shape[0], input.shape[1]).astype(np.int32)
init_h = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)
init_c = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)
P = weight_scale * np.ones((1, number_of_peepholes * hidden_size)).astype(np.float32)
lstm = LSTM_Helper(X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h)
_, Y_h = lstm.step()
expect(node, inputs=[input, W, R, B, seq_lens, init_h, init_c, P], outputs=[Y_h.astype(np.float32)],
name='test_lstm_with_peepholes')
```
### LeakyRelu
There are 2 test cases, listed as following:
leakyrelu
```python
node = onnx.helper.make_node(
'LeakyRelu',
inputs=['x'],
outputs=['y'],
alpha=0.1
)
x = np.array([-1, 0, 1]).astype(np.float32)
# expected output [-0.1, 0., 1.]
y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1
expect(node, inputs=[x], outputs=[y],
name='test_leakyrelu_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1
expect(node, inputs=[x], outputs=[y],
name='test_leakyrelu')
```
leakyrelu_default
```python
default_alpha = 0.01
node = onnx.helper.make_node(
'LeakyRelu',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * default_alpha
expect(node, inputs=[x], outputs=[y],
name='test_leakyrelu_default')
```
### Less
There are 2 test cases, listed as following:
less
```python
node = onnx.helper.make_node(
'Less',
inputs=['x', 'y'],
outputs=['less'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(3, 4, 5).astype(np.float32)
z = np.less(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_less')
```
less_broadcast
```python
node = onnx.helper.make_node(
'Less',
inputs=['x', 'y'],
outputs=['less'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(5).astype(np.float32)
z = np.less(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_less_bcast')
```
### Log
There are 1 test cases, listed as following:
log
```python
node = onnx.helper.make_node(
'Log',
inputs=['x'],
outputs=['y'],
)
x = np.array([1, 10]).astype(np.float32)
y = np.log(x) # expected output [0., 2.30258512]
expect(node, inputs=[x], outputs=[y],
name='test_log_example')
x = np.exp(np.random.randn(3, 4, 5).astype(np.float32))
y = np.log(x)
expect(node, inputs=[x], outputs=[y],
name='test_log')
```
### LogSoftmax
There are 2 test cases, listed as following:
logsoftmax
```python
node = onnx.helper.make_node(
'LogSoftmax',
inputs=['x'],
outputs=['y'],
)
x = np.array([[-1, 0, 1]]).astype(np.float32)
# expected output [[-2.40760589, -1.40760589, -0.40760589]]
y = x - np.log(np.sum(np.exp(x), axis=1))
expect(node, inputs=[x], outputs=[y],
name='test_logsoftmax_example_1')
```
logsoftmax_axis
```python
def logsoftmax_2d(x): # type: (np.ndarray) -> np.ndarray
max_x = np.max(x, axis=1).reshape((-1, 1))
exp_x = np.exp(x - max_x)
return x - max_x - np.log(np.sum(exp_x, axis=1).reshape((-1, 1)))
x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]).astype(np.float32)
# expected output [[-3.4401896, -2.4401896, -1.44018972, -0.44018969],
# [-3.4401896, -2.4401896, -1.44018972, -0.44018969]]
y = logsoftmax_2d(x)
node = onnx.helper.make_node(
'LogSoftmax',
inputs=['x'],
outputs=['y'],
)
expect(node, inputs=[x], outputs=[y],
name='test_logsoftmax_large_number')
x = np.abs(np.random.randn(3, 4, 5).astype(np.float32))
node = onnx.helper.make_node(
'LogSoftmax',
inputs=['x'],
outputs=['y'],
axis=0,
)
y = logsoftmax_2d(x.reshape(1, 60)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_logsoftmax_axis_0')
node = onnx.helper.make_node(
'LogSoftmax',
inputs=['x'],
outputs=['y'],
axis=1,
)
y = logsoftmax_2d(x.reshape(3, 20)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_logsoftmax_axis_1')
# default axis is 1
node = onnx.helper.make_node(
'LogSoftmax',
inputs=['x'],
outputs=['y'],
)
expect(node, inputs=[x], outputs=[y],
name='test_logsoftmax_default_axis')
node = onnx.helper.make_node(
'LogSoftmax',
inputs=['x'],
outputs=['y'],
axis=2,
)
y = logsoftmax_2d(x.reshape(12, 5)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_logsoftmax_axis_2')
node = onnx.helper.make_node(
'LogSoftmax',
inputs=['x'],
outputs=['y'],
axis=-1,
)
y = logsoftmax_2d(x.reshape(12, 5)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_logsoftmax_negative_axis')
```
### MatMul
There are 1 test cases, listed as following:
matmul
```python
node = onnx.helper.make_node(
'MatMul',
inputs=['a', 'b'],
outputs=['c'],
)
# 2d
a = np.random.randn(3, 4).astype(np.float32)
b = np.random.randn(4, 3).astype(np.float32)
c = np.matmul(a, b)
expect(node, inputs=[a, b], outputs=[c],
name='test_matmul_2d')
# 3d
a = np.random.randn(2, 3, 4).astype(np.float32)
b = np.random.randn(2, 4, 3).astype(np.float32)
c = np.matmul(a, b)
expect(node, inputs=[a, b], outputs=[c],
name='test_matmul_3d')
# 4d
a = np.random.randn(1, 2, 3, 4).astype(np.float32)
b = np.random.randn(1, 2, 4, 3).astype(np.float32)
c = np.matmul(a, b)
expect(node, inputs=[a, b], outputs=[c],
name='test_matmul_4d')
```
### MatMulInteger
There are 1 test cases, listed as following:
matmulinteger
```python
node = onnx.helper.make_node('MatMulInteger',
inputs=['A', 'B', 'a_zero_point', 'b_zero_point'],
outputs=['Y'],)
A = np.array([[11, 7, 3],
[10, 6, 2],
[9, 5, 1],
[8, 4, 0], ], dtype=np.uint8)
a_zero_point = np.array([12], dtype=np.uint8)
B = np.array([[1, 4],
[2, 5],
[3, 6], ], dtype=np.uint8)
b_zero_point = np.array([0], dtype=np.uint8)
output = np.array([[-38, -83],
[-44, -98],
[-50, -113],
[-56, -128], ], dtype=np.int32)
expect(node, inputs=[A, B, a_zero_point, b_zero_point], outputs=[output],
name='test_matmulinteger')
```
### Max
There are 1 test cases, listed as following:
max
```python
data_0 = np.array([3, 2, 1]).astype(np.float32)
data_1 = np.array([1, 4, 4]).astype(np.float32)
data_2 = np.array([2, 5, 3]).astype(np.float32)
result = np.array([3, 5, 4]).astype(np.float32)
node = onnx.helper.make_node(
'Max',
inputs=['data_0', 'data_1', 'data_2'],
outputs=['result'],
)
expect(node, inputs=[data_0, data_1, data_2], outputs=[result],
name='test_max_example')
node = onnx.helper.make_node(
'Max',
inputs=['data_0'],
outputs=['result'],
)
expect(node, inputs=[data_0], outputs=[data_0],
name='test_max_one_input')
result = np.maximum(data_0, data_1)
node = onnx.helper.make_node(
'Max',
inputs=['data_0', 'data_1'],
outputs=['result'],
)
expect(node, inputs=[data_0, data_1], outputs=[result],
name='test_max_two_inputs')
```
### MaxPool
There are 14 test cases, listed as following:
maxpool_1d_default
```python
"""
input_shape: [1, 3, 32]
output_shape: [1, 3, 31]
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2],
)
x = np.random.randn(1, 3, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = [2]
strides = [1]
out_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)
padded = x
y = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'MAX')
expect(node, inputs=[x], outputs=[y], name='test_maxpool_1d_default')
```
maxpool_2d_ceil
```python
"""
input_shape: [1, 1, 4, 4]
output_shape: [1, 1, 2, 2]
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[3, 3],
strides=[2, 2],
ceil_mode=True
)
x = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]]).astype(np.float32)
y = np.array([[[
[11, 12],
[15, 16]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_ceil')
```
maxpool_2d_default
```python
"""
input_shape: [1, 3, 32, 32]
output_shape: [1, 3, 31, 31]
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (2, 2)
strides = (1, 1)
out_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)
padded = x
y = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_default')
```
maxpool_2d_dilations
```python
"""
input_shape: [1, 1, 4, 4]
output_shape: [1, 1, 2, 2]
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
strides=[1, 1],
dilations=[2, 2]
)
x = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]]).astype(np.float32)
y = np.array([[[
[11, 12],
[15, 16]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_dilations')
```
maxpool_2d_pads
```python
"""
input_shape: [1, 3, 28, 28]
output_shape: [1, 3, 30, 30]
pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[3, 3],
pads=[2, 2, 2, 2]
)
x = np.random.randn(1, 3, 28, 28).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (3, 3)
strides = (1, 1)
pad_bottom = pad_top = pad_right = pad_left = 2
pad_shape = [pad_top + pad_bottom, pad_left + pad_right]
out_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)
padded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',
constant_values=np.nan)
y = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_pads')
```
maxpool_2d_precomputed_pads
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 5, 5]
pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[5, 5],
pads=[2, 2, 2, 2]
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[
[13, 14, 15, 15, 15],
[18, 19, 20, 20, 20],
[23, 24, 25, 25, 25],
[23, 24, 25, 25, 25],
[23, 24, 25, 25, 25]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_pads')
```
maxpool_2d_precomputed_same_upper
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 3, 3]
pad_shape: [2, 2] -> [1, 1, 1, 1] by axis
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[3, 3],
strides=[2, 2],
auto_pad='SAME_UPPER'
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[[7, 9, 10],
[17, 19, 20],
[22, 24, 25]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_same_upper')
```
maxpool_2d_precomputed_strides
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 2, 2]
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
strides=[2, 2]
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[[7, 9],
[17, 19]]]]).astype(np.float32)
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_strides')
```
maxpool_2d_same_lower
```python
"""
input_shape: [1, 3, 32, 32]
output_shape: [1, 3, 32, 32]
pad_shape: [1, 1] -> [1, 0, 1, 0] by axis
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
auto_pad='SAME_LOWER'
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (2, 2)
strides = (1, 1)
out_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)
pad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)
pad_bottom = pad_shape[0] // 2
pad_top = pad_shape[0] - pad_bottom
pad_right = pad_shape[1] // 2
pad_left = pad_shape[1] - pad_right
padded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',
constant_values=np.nan)
y = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_lower')
```
maxpool_2d_same_upper
```python
"""
input_shape: [1, 3, 32, 32]
output_shape: [1, 3, 32, 32]
pad_shape: [1, 1] -> [0, 1, 0, 1] by axis
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2],
auto_pad='SAME_UPPER'
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (2, 2)
strides = (1, 1)
out_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)
pad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)
pad_top = pad_shape[0] // 2
pad_bottom = pad_shape[0] - pad_top
pad_left = pad_shape[1] // 2
pad_right = pad_shape[1] - pad_left
padded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',
constant_values=np.nan)
y = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_upper')
```
maxpool_2d_strides
```python
"""
input_shape: [1, 3, 32, 32]
output_shape: [1, 3, 10, 10]
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[5, 5],
strides=[3, 3]
)
x = np.random.randn(1, 3, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = (5, 5)
strides = (3, 3)
out_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)
padded = x
y = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')
expect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_strides')
```
maxpool_3d_default
```python
"""
input_shape: [1, 3, 32, 32, 32]
output_shape: [1, 3, 31, 31, 31]
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y'],
kernel_shape=[2, 2, 2],
)
x = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)
x_shape = np.shape(x)
kernel_shape = [2, 2, 2]
strides = [1, 1, 1]
out_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)
padded = x
y = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'MAX')
expect(node, inputs=[x], outputs=[y], name='test_maxpool_3d_default')
```
maxpool_with_argmax_2d_precomputed_pads
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 5, 5]
pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y', 'z'],
kernel_shape=[5, 5],
pads=[2, 2, 2, 2]
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[
[13, 14, 15, 15, 15],
[18, 19, 20, 20, 20],
[23, 24, 25, 25, 25],
[23, 24, 25, 25, 25],
[23, 24, 25, 25, 25]]]]).astype(np.float32)
z = np.array([[[
[12, 13, 14, 14, 14],
[17, 18, 19, 19, 19],
[22, 23, 24, 24, 24],
[22, 23, 24, 24, 24],
[22, 23, 24, 24, 24]]]]).astype(np.int64)
expect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_pads')
```
maxpool_with_argmax_2d_precomputed_strides
```python
"""
input_shape: [1, 1, 5, 5]
output_shape: [1, 1, 2, 2]
"""
node = onnx.helper.make_node(
'MaxPool',
inputs=['x'],
outputs=['y', 'z'],
kernel_shape=[2, 2],
strides=[2, 2],
storage_order=1
)
x = np.array([[[
[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],
]]]).astype(np.float32)
y = np.array([[[[7, 9],
[17, 19]]]]).astype(np.float32)
z = np.array([[[[6, 16],
[8, 18]]]]).astype(np.int64)
expect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_strides')
```
### MaxUnpool
There are 2 test cases, listed as following:
with_output_shape
```python
node = onnx.helper.make_node(
'MaxUnpool',
inputs=['xT', 'xI', 'output_shape'],
outputs=['y'],
kernel_shape=[2, 2],
strides=[2, 2]
)
xT = np.array([[[[5, 6],
[7, 8]]]], dtype=np.float32)
xI = np.array([[[[5, 7],
[13, 15]]]], dtype=np.int64)
output_shape = np.array((1, 1, 5, 5), dtype=np.int64)
y = np.array([[[[0, 0, 0, 0, 0],
[0, 5, 0, 6, 0],
[0, 0, 0, 0, 0],
[0, 7, 0, 8, 0],
[0, 0, 0, 0, 0]]]], dtype=np.float32)
expect(node, inputs=[xT, xI, output_shape], outputs=[y], name='test_maxunpool_export_with_output_shape')
```
without_output_shape
```python
node = onnx.helper.make_node(
'MaxUnpool',
inputs=['xT', 'xI'],
outputs=['y'],
kernel_shape=[2, 2],
strides=[2, 2]
)
xT = np.array([[[[1, 2],
[3, 4]]]], dtype=np.float32)
xI = np.array([[[[5, 7],
[13, 15]]]], dtype=np.int64)
y = np.array([[[[0, 0, 0, 0],
[0, 1, 0, 2],
[0, 0, 0, 0],
[0, 3, 0, 4]]]], dtype=np.float32)
expect(node, inputs=[xT, xI], outputs=[y], name='test_maxunpool_export_without_output_shape')
```
### Mean
There are 1 test cases, listed as following:
mean
```python
data_0 = np.array([3, 0, 2]).astype(np.float32)
data_1 = np.array([1, 3, 4]).astype(np.float32)
data_2 = np.array([2, 6, 6]).astype(np.float32)
result = np.array([2, 3, 4]).astype(np.float32)
node = onnx.helper.make_node(
'Mean',
inputs=['data_0', 'data_1', 'data_2'],
outputs=['result'],
)
expect(node, inputs=[data_0, data_1, data_2], outputs=[result],
name='test_mean_example')
node = onnx.helper.make_node(
'Mean',
inputs=['data_0'],
outputs=['result'],
)
expect(node, inputs=[data_0], outputs=[data_0],
name='test_mean_one_input')
result = np.divide(np.add(data_0, data_1), 2.)
node = onnx.helper.make_node(
'Mean',
inputs=['data_0', 'data_1'],
outputs=['result'],
)
expect(node, inputs=[data_0, data_1], outputs=[result],
name='test_mean_two_inputs')
```
### MeanVarianceNormalization
There are 1 test cases, listed as following:
meanvariancenormalization
```python
node = onnx.helper.make_node(
'MeanVarianceNormalization',
inputs=['X'],
outputs=['Y']
)
input_data = np.array([[[[0.8439683], [0.5665144], [0.05836735]],
[[0.02916367], [0.12964272], [0.5060197]],
[[0.79538304], [0.9411346], [0.9546573]]],
[[[0.17730942], [0.46192095], [0.26480448]],
[[0.6746842], [0.01665257], [0.62473077]],
[[0.9240844], [0.9722341], [0.11965699]]],
[[[0.41356155], [0.9129373], [0.59330076]],
[[0.81929934], [0.7862604], [0.11799799]],
[[0.69248444], [0.54119414], [0.07513223]]]], dtype=np.float32)
# Calculate expected output data
data_mean = np.mean(input_data, axis=(0, 2, 3), keepdims=1)
data_mean_squared = np.power(data_mean, 2)
data_squared = np.power(input_data, 2)
data_squared_mean = np.mean(data_squared, axis=(0, 2, 3), keepdims=1)
std = np.sqrt(data_squared_mean - data_mean_squared)
expected_output = (input_data - data_mean) / (std + 1e-9)
expect(node, inputs=[input_data], outputs=[expected_output],
name='test_mvn')
```
### Min
There are 1 test cases, listed as following:
min
```python
data_0 = np.array([3, 2, 1]).astype(np.float32)
data_1 = np.array([1, 4, 4]).astype(np.float32)
data_2 = np.array([2, 5, 0]).astype(np.float32)
result = np.array([1, 2, 0]).astype(np.float32)
node = onnx.helper.make_node(
'Min',
inputs=['data_0', 'data_1', 'data_2'],
outputs=['result'],
)
expect(node, inputs=[data_0, data_1, data_2], outputs=[result],
name='test_min_example')
node = onnx.helper.make_node(
'Min',
inputs=['data_0'],
outputs=['result'],
)
expect(node, inputs=[data_0], outputs=[data_0],
name='test_min_one_input')
result = np.minimum(data_0, data_1)
node = onnx.helper.make_node(
'Min',
inputs=['data_0', 'data_1'],
outputs=['result'],
)
expect(node, inputs=[data_0, data_1], outputs=[result],
name='test_min_two_inputs')
```
### Mod
There are 13 test cases, listed as following:
mod_broadcast
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.arange(0, 30).reshape([3, 2, 5])
y = np.array([7])
z = np.mod(x, y)
z
# array([[[0, 1, 2, 3, 4],
# [5, 6, 0, 1, 2]],
# [[3, 4, 5, 6, 0],
# [1, 2, 3, 4, 5]],
# [[6, 0, 1, 2, 3],
# [4, 5, 6, 0, 1]]], dtype=int32)
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_broadcast')
```
mod_int64_fmod
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
fmod=1
)
x = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int64)
y = np.array([2, -3, 8, -2, 3, 5]).astype(np.int64)
z = np.fmod(x, y) # expected output [ 0, 1, 5, 0, -1, 3]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_int64_fmod')
```
mod_mixed_sign_float16
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
fmod=1
)
x = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float16)
y = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float16)
z = np.fmod(x, y) # expected output [-0.10156, 0.3984 , 5. , 0.10156, -0.3984 , 3.]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_mixed_sign_float16')
```
mod_mixed_sign_float32
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
fmod=1
)
x = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float32)
y = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float32)
z = np.fmod(x, y) # expected output [-0.10000038, 0.39999962, 5. , 0.10000038, -0.39999962, 3.]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_mixed_sign_float32')
```
mod_mixed_sign_float64
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
fmod=1
)
x = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float64)
y = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float64)
z = np.fmod(x, y) # expected output [-0.1, 0.4, 5. , 0.1, -0.4, 3.]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_mixed_sign_float64')
```
mod_mixed_sign_int16
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int16)
y = np.array([2, -3, 8, -2, 3, 5]).astype(np.int16)
z = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_mixed_sign_int16')
```
mod_mixed_sign_int32
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int32)
y = np.array([2, -3, 8, -2, 3, 5]).astype(np.int32)
z = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_mixed_sign_int32')
```
mod_mixed_sign_int64
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int64)
y = np.array([2, -3, 8, -2, 3, 5]).astype(np.int64)
z = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_mixed_sign_int64')
```
mod_mixed_sign_int8
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int8)
y = np.array([2, -3, 8, -2, 3, 5]).astype(np.int8)
z = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_mixed_sign_int8')
```
mod_uint16
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([4, 7, 5]).astype(np.uint16)
y = np.array([2, 3, 8]).astype(np.uint16)
z = np.mod(x, y) # expected output [0, 1, 5]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_uint16')
```
mod_uint32
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([4, 7, 5]).astype(np.uint32)
y = np.array([2, 3, 8]).astype(np.uint32)
z = np.mod(x, y) # expected output [0, 1, 5]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_uint32')
```
mod_uint64
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([4, 7, 5]).astype(np.uint64)
y = np.array([2, 3, 8]).astype(np.uint64)
z = np.mod(x, y) # expected output [0, 1, 5]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_uint64')
```
mod_uint8
```python
node = onnx.helper.make_node(
'Mod',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([4, 7, 5]).astype(np.uint8)
y = np.array([2, 3, 8]).astype(np.uint8)
z = np.mod(x, y) # expected output [0, 1, 5]
expect(node, inputs=[x, y], outputs=[z],
name='test_mod_uint8')
```
### Mul
There are 2 test cases, listed as following:
mul
```python
node = onnx.helper.make_node(
'Mul',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([4, 5, 6]).astype(np.float32)
z = x * y # expected output [4., 10., 18.]
expect(node, inputs=[x, y], outputs=[z],
name='test_mul_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(3, 4, 5).astype(np.float32)
z = x * y
expect(node, inputs=[x, y], outputs=[z],
name='test_mul')
```
mul_broadcast
```python
node = onnx.helper.make_node(
'Mul',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(5).astype(np.float32)
z = x * y
expect(node, inputs=[x, y], outputs=[z],
name='test_mul_bcast')
```
### Neg
There are 1 test cases, listed as following:
neg
```python
node = onnx.helper.make_node(
'Neg',
inputs=['x'],
outputs=['y'],
)
x = np.array([-4, 2]).astype(np.float32)
y = np.negative(x) # expected output [4., -2.],
expect(node, inputs=[x], outputs=[y],
name='test_neg_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.negative(x)
expect(node, inputs=[x], outputs=[y],
name='test_neg')
```
### NonMaxSuppression
There are 9 test cases, listed as following:
nonmaxsuppression_center_point_box_format
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices'],
center_point_box=1
)
boxes = np.array([[
[0.5, 0.5, 1.0, 1.0],
[0.5, 0.6, 1.0, 1.0],
[0.5, 0.4, 1.0, 1.0],
[0.5, 10.5, 1.0, 1.0],
[0.5, 10.6, 1.0, 1.0],
[0.5, 100.5, 1.0, 1.0]
]]).astype(np.float32)
scores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)
max_output_boxes_per_class = np.array([3]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.0]).astype(np.float32)
selected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_center_point_box_format')
```
nonmaxsuppression_flipped_coordinates
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices']
)
boxes = np.array([[
[1.0, 1.0, 0.0, 0.0],
[0.0, 0.1, 1.0, 1.1],
[0.0, 0.9, 1.0, -0.1],
[0.0, 10.0, 1.0, 11.0],
[1.0, 10.1, 0.0, 11.1],
[1.0, 101.0, 0.0, 100.0]
]]).astype(np.float32)
scores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)
max_output_boxes_per_class = np.array([3]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.0]).astype(np.float32)
selected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_flipped_coordinates')
```
nonmaxsuppression_identical_boxes
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices']
)
boxes = np.array([[
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0]
]]).astype(np.float32)
scores = np.array([[[0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]]]).astype(np.float32)
max_output_boxes_per_class = np.array([3]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.0]).astype(np.float32)
selected_indices = np.array([[0, 0, 0]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_identical_boxes')
```
nonmaxsuppression_limit_output_size
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices']
)
boxes = np.array([[
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.1, 1.0, 1.1],
[0.0, -0.1, 1.0, 0.9],
[0.0, 10.0, 1.0, 11.0],
[0.0, 10.1, 1.0, 11.1],
[0.0, 100.0, 1.0, 101.0]
]]).astype(np.float32)
scores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)
max_output_boxes_per_class = np.array([2]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.0]).astype(np.float32)
selected_indices = np.array([[0, 0, 3], [0, 0, 0]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_limit_output_size')
```
nonmaxsuppression_single_box
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices']
)
boxes = np.array([[
[0.0, 0.0, 1.0, 1.0]
]]).astype(np.float32)
scores = np.array([[[0.9]]]).astype(np.float32)
max_output_boxes_per_class = np.array([3]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.0]).astype(np.float32)
selected_indices = np.array([[0, 0, 0]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_single_box')
```
nonmaxsuppression_suppress_by_IOU
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices']
)
boxes = np.array([[
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.1, 1.0, 1.1],
[0.0, -0.1, 1.0, 0.9],
[0.0, 10.0, 1.0, 11.0],
[0.0, 10.1, 1.0, 11.1],
[0.0, 100.0, 1.0, 101.0]
]]).astype(np.float32)
scores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)
max_output_boxes_per_class = np.array([3]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.0]).astype(np.float32)
selected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_suppress_by_IOU')
```
nonmaxsuppression_suppress_by_IOU_and_scores
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices']
)
boxes = np.array([[
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.1, 1.0, 1.1],
[0.0, -0.1, 1.0, 0.9],
[0.0, 10.0, 1.0, 11.0],
[0.0, 10.1, 1.0, 11.1],
[0.0, 100.0, 1.0, 101.0]
]]).astype(np.float32)
scores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)
max_output_boxes_per_class = np.array([3]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.4]).astype(np.float32)
selected_indices = np.array([[0, 0, 3], [0, 0, 0]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_suppress_by_IOU_and_scores')
```
nonmaxsuppression_two_batches
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices']
)
boxes = np.array([[[0.0, 0.0, 1.0, 1.0],
[0.0, 0.1, 1.0, 1.1],
[0.0, -0.1, 1.0, 0.9],
[0.0, 10.0, 1.0, 11.0],
[0.0, 10.1, 1.0, 11.1],
[0.0, 100.0, 1.0, 101.0]],
[[0.0, 0.0, 1.0, 1.0],
[0.0, 0.1, 1.0, 1.1],
[0.0, -0.1, 1.0, 0.9],
[0.0, 10.0, 1.0, 11.0],
[0.0, 10.1, 1.0, 11.1],
[0.0, 100.0, 1.0, 101.0]]]).astype(np.float32)
scores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]],
[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)
max_output_boxes_per_class = np.array([2]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.0]).astype(np.float32)
selected_indices = np.array([[0, 0, 3], [0, 0, 0], [1, 0, 3], [1, 0, 0]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_two_batches')
```
nonmaxsuppression_two_classes
```python
node = onnx.helper.make_node(
'NonMaxSuppression',
inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],
outputs=['selected_indices']
)
boxes = np.array([[
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.1, 1.0, 1.1],
[0.0, -0.1, 1.0, 0.9],
[0.0, 10.0, 1.0, 11.0],
[0.0, 10.1, 1.0, 11.1],
[0.0, 100.0, 1.0, 101.0]
]]).astype(np.float32)
scores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3],
[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)
max_output_boxes_per_class = np.array([2]).astype(np.int64)
iou_threshold = np.array([0.5]).astype(np.float32)
score_threshold = np.array([0.0]).astype(np.float32)
selected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 1, 3], [0, 1, 0]]).astype(np.int64)
expect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_two_classes')
```
### NonZero
There are 1 test cases, listed as following:
nonzero
```python
node = onnx.helper.make_node(
'NonZero',
inputs=['condition'],
outputs=['result'],
)
condition = np.array([[1, 0], [1, 1]], dtype=np.bool)
result = np.array((np.nonzero(condition))) # expected output [[0, 1, 1], [0, 0, 1]]
expect(node, inputs=[condition], outputs=[result],
name='test_nonzero_example')
```
### Not
There are 1 test cases, listed as following:
not
```python
node = onnx.helper.make_node(
'Not',
inputs=['x'],
outputs=['not'],
)
# 2d
x = (np.random.randn(3, 4) > 0).astype(np.bool)
expect(node, inputs=[x], outputs=[np.logical_not(x)],
name='test_not_2d')
# 3d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
expect(node, inputs=[x], outputs=[np.logical_not(x)],
name='test_not_3d')
# 4d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
expect(node, inputs=[x], outputs=[np.logical_not(x)],
name='test_not_4d')
```
### OneHot
There are 4 test cases, listed as following:
with_axis
```python
axisValue = 1
on_value = 3
off_value = 1
output_type = np.float32
node = onnx.helper.make_node(
'OneHot',
inputs=['indices', 'depth', 'values'],
outputs=['y'],
axis=axisValue
)
indices = np.array([[1, 9],
[2, 4]], dtype=np.float32)
depth = np.array([10], dtype=np.float32)
values = np.array([off_value, on_value], dtype=output_type)
y = one_hot(indices, depth, axis=axisValue, dtype=output_type)
y = y * (on_value - off_value) + off_value
expect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_with_axis')
```
with_negative_axis
```python
axisValue = -2
on_value = 3
off_value = 1
output_type = np.float32
node = onnx.helper.make_node(
'OneHot',
inputs=['indices', 'depth', 'values'],
outputs=['y'],
axis=axisValue
)
indices = np.array([[1, 9],
[2, 4]], dtype=np.float32)
depth = np.array([10], dtype=np.float32)
values = np.array([off_value, on_value], dtype=output_type)
y = one_hot(indices, depth, axis=axisValue, dtype=output_type)
y = y * (on_value - off_value) + off_value
expect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_with_negative_axis')
```
with_negative_indices
```python
axisValue = 1
on_value = 3
off_value = 1
output_type = np.float32
node = onnx.helper.make_node(
'OneHot',
inputs=['indices', 'depth', 'values'],
outputs=['y'],
axis=axisValue
)
indices = np.array([0, -7, -8], dtype=np.int64)
depth = np.array([10], dtype=np.float32)
values = np.array([off_value, on_value], dtype=output_type)
y = one_hot(indices, depth, axis=axisValue, dtype=output_type)
y = y * (on_value - off_value) + off_value
expect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_negative_indices')
```
without_axis
```python
on_value = 5
off_value = 2
output_type = np.int32
node = onnx.helper.make_node(
'OneHot',
inputs=['indices', 'depth', 'values'],
outputs=['y']
)
indices = np.array([0, 7, 8], dtype=np.int64)
depth = np.float32(12)
values = np.array([off_value, on_value], dtype=output_type)
y = one_hot(indices, depth, dtype=output_type)
y = y * (on_value - off_value) + off_value
expect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_without_axis')
```
### Or
There are 2 test cases, listed as following:
or
```python
node = onnx.helper.make_node(
'Or',
inputs=['x', 'y'],
outputs=['or'],
)
# 2d
x = (np.random.randn(3, 4) > 0).astype(np.bool)
y = (np.random.randn(3, 4) > 0).astype(np.bool)
z = np.logical_or(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_or2d')
# 3d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
z = np.logical_or(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_or3d')
# 4d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
z = np.logical_or(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_or4d')
```
or_broadcast
```python
node = onnx.helper.make_node(
'Or',
inputs=['x', 'y'],
outputs=['or'],
)
# 3d vs 1d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(5) > 0).astype(np.bool)
z = np.logical_or(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_or_bcast3v1d')
# 3d vs 2d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(4, 5) > 0).astype(np.bool)
z = np.logical_or(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_or_bcast3v2d')
# 4d vs 2d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(5, 6) > 0).astype(np.bool)
z = np.logical_or(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_or_bcast4v2d')
# 4d vs 3d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(4, 5, 6) > 0).astype(np.bool)
z = np.logical_or(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_or_bcast4v3d')
# 4d vs 4d
x = (np.random.randn(1, 4, 1, 6) > 0).astype(np.bool)
y = (np.random.randn(3, 1, 5, 6) > 0).astype(np.bool)
z = np.logical_or(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_or_bcast4v4d')
```
### PRelu
There are 2 test cases, listed as following:
prelu
```python
node = onnx.helper.make_node(
'PRelu',
inputs=['x', 'slope'],
outputs=['y'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
slope = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope
expect(node, inputs=[x, slope], outputs=[y],
name='test_prelu_example')
```
prelu_broadcast
```python
node = onnx.helper.make_node(
'PRelu',
inputs=['x', 'slope'],
outputs=['y'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
slope = np.random.randn(5).astype(np.float32)
y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope
expect(node, inputs=[x, slope], outputs=[y],
name='test_prelu_broadcast')
```
### Pad
There are 2 test cases, listed as following:
constant_pad
```python
node = onnx.helper.make_node(
'Pad',
inputs=['x', 'pads', 'value'],
outputs=['y'],
mode='constant'
)
x = np.random.randn(1, 3, 4, 5).astype(np.float32)
pads = np.array([0, 0, 1, 3, 0, 0, 2, 4]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]
value = np.float32(1.2)
y = pad_impl(
x,
pads,
'constant',
1.2
)
expect(node, inputs=[x, pads, value], outputs=[y],
name='test_constant_pad')
```
reflection_and_edge_pad
```python
for mode in ['edge', 'reflect']:
node = onnx.helper.make_node(
'Pad',
inputs=['x', 'pads'],
outputs=['y'],
mode=mode
)
x = np.random.randn(1, 3, 4, 5).astype(np.int32)
pads = np.array([0, 0, 1, 1, 0, 0, 1, 1]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]
y = pad_impl(
x,
pads,
mode
)
expect(node, inputs=[x, pads], outputs=[y],
name='test_{}_pad'.format(mode))
```
### Pow
There are 2 test cases, listed as following:
pow
```python
node = onnx.helper.make_node(
'Pow',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([4, 5, 6]).astype(np.float32)
z = np.power(x, y) # expected output [1., 32., 729.]
expect(node, inputs=[x, y], outputs=[z],
name='test_pow_example')
x = np.arange(60).reshape(3, 4, 5).astype(np.float32)
y = np.random.randn(3, 4, 5).astype(np.float32)
z = np.power(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_pow')
```
pow_broadcast
```python
node = onnx.helper.make_node(
'Pow',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([1, 2, 3]).astype(np.float32)
y = np.array(2).astype(np.float32)
z = np.power(x, y) # expected output [1., 4., 9.]
expect(node, inputs=[x, y], outputs=[z],
name='test_pow_bcast_scalar')
node = onnx.helper.make_node(
'Pow',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
y = np.array([1, 2, 3]).astype(np.float32)
# expected output [[1, 4, 27], [4, 25, 216]]
z = np.power(x, y).astype(np.float32)
expect(node, inputs=[x, y], outputs=[z],
name='test_pow_bcast_array')
```
### QLinearConv
There are 1 test cases, listed as following:
qlinearconv
```python
node = onnx.helper.make_node('QLinearConv',
inputs=['x', 'x_scale', 'x_zero_point', 'w', 'w_scale', 'w_zero_point', 'y_scale', 'y_zero_point'],
outputs=['y'],)
x = np.array([[255, 174, 162, 25, 203, 168, 58],
[15, 59, 237, 95, 129, 0, 64],
[56, 242, 153, 221, 168, 12, 166],
[232, 178, 186, 195, 237, 162, 237],
[188, 39, 124, 77, 80, 102, 43],
[127, 230, 21, 83, 41, 40, 134],
[255, 154, 92, 141, 42, 148, 247], ], dtype=np.uint8).reshape((1, 1, 7, 7))
x_scale = np.float32(0.00369204697)
x_zero_point = np.uint8(132)
w = np.array([0], dtype=np.uint8).reshape((1, 1, 1, 1))
w_scale = np.array([0.00172794575], dtype=np.float32)
w_zero_point = np.array([255], dtype=np.uint8)
y_scale = np.float32(0.00162681262)
y_zero_point = np.uint8(123)
output = np.array([[0, 81, 93, 230, 52, 87, 197],
[240, 196, 18, 160, 126, 255, 191],
[199, 13, 102, 34, 87, 243, 89],
[23, 77, 69, 60, 18, 93, 18],
[67, 216, 131, 178, 175, 153, 212],
[128, 25, 234, 172, 214, 215, 121],
[0, 101, 163, 114, 213, 107, 8], ], dtype=np.uint8).reshape((1, 1, 7, 7))
expect(node, inputs=[x, x_scale, x_zero_point, w, w_scale, w_zero_point, y_scale, y_zero_point], outputs=[output],
name='test_qlinearconv')
```
### QLinearMatMul
There are 1 test cases, listed as following:
qlinearmatmul
```python
node = onnx.helper.make_node('QLinearMatMul',
inputs=['a', 'a_scale', 'a_zero_point', 'b', 'b_scale', 'b_zero_point', 'y_scale', 'y_zero_point'],
outputs=['y'],)
#2D
a = np.array([[208, 236, 0, 238],
[3, 214, 255, 29], ], dtype=np.uint8)
a_scale = np.array([0.0066], dtype=np.float32)
a_zero_point = np.array([113], dtype=np.uint8)
b = np.array([[152, 51, 244],
[60, 26, 255],
[0, 127, 246],
[127, 254, 247]], dtype=np.uint8)
b_scale = np.array([0.00705], dtype=np.float32)
b_zero_point = np.array([114], dtype=np.uint8)
y_scale = np.array([0.0107], dtype=np.float32)
y_zero_point = np.array([118], dtype=np.uint8)
output = np.array([[168, 115, 255],
[1, 66, 151], ], dtype=np.uint8)
expect(node, inputs=[a, a_scale, a_zero_point, b, b_scale, b_zero_point, y_scale, y_zero_point], outputs=[output],
name='test_qlinearmatmul_2D')
#3D
a = np.array([[[208, 236, 0, 238],
[3, 214, 255, 29]],
[[208, 236, 0, 238],
[3, 214, 255, 29]]], dtype=np.uint8)
a_scale = np.array([0.0066], dtype=np.float32)
a_zero_point = np.array([113], dtype=np.uint8)
b = np.array([[[152, 51, 244],
[60, 26, 255],
[0, 127, 246],
[127, 254, 247]],
[[152, 51, 244],
[60, 26, 255],
[0, 127, 246],
[127, 254, 247]]], dtype=np.uint8)
b_scale = np.array([0.00705], dtype=np.float32)
b_zero_point = np.array([114], dtype=np.uint8)
y_scale = np.array([0.0107], dtype=np.float32)
y_zero_point = np.array([118], dtype=np.uint8)
output = np.array([[[168, 115, 255],
[1, 66, 151]],
[[168, 115, 255],
[1, 66, 151]]], dtype=np.uint8)
expect(node, inputs=[a, a_scale, a_zero_point, b, b_scale, b_zero_point, y_scale, y_zero_point], outputs=[output],
name='test_qlinearmatmul_3D')
```
### QuantizeLinear
There are 1 test cases, listed as following:
quantizelinear
```python
node = onnx.helper.make_node('QuantizeLinear',
inputs=['x', 'y_scale', 'y_zero_point'],
outputs=['y'],)
x = np.array([0, 2, 3, 1000, -254, -1000]).astype(np.float32)
y_scale = np.float32(2)
y_zero_point = np.uint8(128)
y = np.array([128, 129, 130, 255, 1, 0]).astype(np.uint8)
expect(node, inputs=[x, y_scale, y_zero_point], outputs=[y],
name='test_quantizelinear')
```
### RNN
There are 3 test cases, listed as following:
defaults
```python
input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)
input_size = 2
hidden_size = 4
weight_scale = 0.1
node = onnx.helper.make_node(
'RNN',
inputs=['X', 'W', 'R'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
W = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)
rnn = RNN_Helper(X=input, W=W, R=R)
_, Y_h = rnn.step()
expect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_simple_rnn_defaults')
```
initial_bias
```python
input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)
input_size = 3
hidden_size = 5
custom_bias = 0.1
weight_scale = 0.1
node = onnx.helper.make_node(
'RNN',
inputs=['X', 'W', 'R', 'B'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
W = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)
# Adding custom bias
W_B = custom_bias * np.ones((1, hidden_size)).astype(np.float32)
R_B = np.zeros((1, hidden_size)).astype(np.float32)
B = np.concatenate((W_B, R_B), axis=1)
rnn = RNN_Helper(X=input, W=W, R=R, B=B)
_, Y_h = rnn.step()
expect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)],
name='test_simple_rnn_with_initial_bias')
```
seq_length
```python
input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
[[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)
input_size = 3
hidden_size = 5
node = onnx.helper.make_node(
'RNN',
inputs=['X', 'W', 'R', 'B'],
outputs=['', 'Y'],
hidden_size=hidden_size
)
W = np.random.randn(1, hidden_size, input_size).astype(np.float32)
R = np.random.randn(1, hidden_size, hidden_size).astype(np.float32)
# Adding custom bias
W_B = np.random.randn(1, hidden_size).astype(np.float32)
R_B = np.random.randn(1, hidden_size).astype(np.float32)
B = np.concatenate((W_B, R_B), axis=1)
rnn = RNN_Helper(X=input, W=W, R=R, B=B)
_, Y_h = rnn.step()
expect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_rnn_seq_length')
```
### Range
There are 2 test cases, listed as following:
range_float_type_positive_delta
```python
node = onnx.helper.make_node(
'Range',
inputs=['start', 'limit', 'delta'],
outputs=['output'],
)
start = np.float32(1)
limit = np.float32(5)
delta = np.float32(2)
output = np.arange(start, limit, delta, dtype=np.float32) # expected output [1.0, 3.0]
expect(node, inputs=[start, limit, delta], outputs=[output],
name='test_range_float_type_positive_delta')
```
range_int32_type_negative_delta
```python
node = onnx.helper.make_node(
'Range',
inputs=['start', 'limit', 'delta'],
outputs=['output'],
)
start = np.int32(10)
limit = np.int32(6)
delta = np.int32(-3)
output = np.arange(start, limit, delta, dtype=np.int32) # expected output [10, 7]
expect(node, inputs=[start, limit, delta], outputs=[output],
name='test_range_int32_type_negative_delta')
```
### Reciprocal
There are 1 test cases, listed as following:
reciprocal
```python
node = onnx.helper.make_node(
'Reciprocal',
inputs=['x'],
outputs=['y'],
)
x = np.array([-4, 2]).astype(np.float32)
y = np.reciprocal(x) # expected output [-0.25, 0.5],
expect(node, inputs=[x], outputs=[y],
name='test_reciprocal_example')
x = np.random.rand(3, 4, 5).astype(np.float32) + 0.5
y = np.reciprocal(x)
expect(node, inputs=[x], outputs=[y],
name='test_reciprocal')
```
### ReduceL1
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceL1',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims
)
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
reduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)
#print(reduced)
#[[[78.]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l1_default_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l1_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [2]
keepdims = 0
node = onnx.helper.make_node(
'ReduceL1',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[3., 7.], [11., 15.], [19., 23.]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l1_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l1_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceL1',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l1_keep_dims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l1_keep_dims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceL1',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
# print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
# print(reduced)
#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l1_negative_axes_keep_dims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l1_negative_axes_keep_dims_random')
```
### ReduceL2
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceL2',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims
)
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
reduced = np.sqrt(np.sum(
a=np.square(data), axis=axes, keepdims=keepdims == 1))
#print(reduced)
#[[[25.49509757]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l2_default_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sqrt(np.sum(
a=np.square(data), axis=axes, keepdims=keepdims == 1))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l2_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [2]
keepdims = 0
node = onnx.helper.make_node(
'ReduceL2',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
reduced = np.sqrt(np.sum(
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
#print(reduced)
#[[2.23606798, 5.],
# [7.81024968, 10.63014581],
# [13.45362405, 16.2788206]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l2_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sqrt(np.sum(
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l2_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceL2',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
reduced = np.sqrt(np.sum(
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
#print(reduced)
#[[[2.23606798], [5.]]
# [[7.81024968], [10.63014581]]
# [[13.45362405], [16.2788206 ]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l2_keep_dims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sqrt(np.sum(
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_l2_keep_dims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceL2',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
# print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
reduced = np.sqrt(np.sum(
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
# print(reduced)
#[[[2.23606798], [5.]]
# [[7.81024968], [10.63014581]]
# [[13.45362405], [16.2788206 ]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l2_negative_axes_keep_dims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sqrt(np.sum(
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_l2_negative_axes_keep_dims_random')
```
### ReduceLogSum
There are 3 test cases, listed as following:
keepdims
```python
node = onnx.helper.make_node(
'ReduceLogSum',
inputs=['data'],
outputs=["reduced"]
)
data = np.random.ranf([3, 4, 5]).astype(np.float32)
reduced = np.log(np.sum(data, keepdims=True))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_default')
```
negative_axes_keepdims
```python
node = onnx.helper.make_node(
'ReduceLogSum',
inputs=['data'],
outputs=["reduced"],
axes=[-2]
)
data = np.random.ranf([3, 4, 5]).astype(np.float32)
reduced = np.log(np.sum(data, axis=(-2), keepdims=True))
# print(reduced)
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_negative_axes')
```
nokeepdims
```python
node = onnx.helper.make_node(
'ReduceLogSum',
inputs=['data'],
outputs=["reduced"],
axes=[2, 1],
keepdims=0
)
data = np.random.ranf([3, 4, 5]).astype(np.float32)
reduced = np.log(np.sum(data, axis=(2, 1), keepdims=False))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_desc_axes')
node = onnx.helper.make_node(
'ReduceLogSum',
inputs=['data'],
outputs=["reduced"],
axes=[0, 1],
keepdims=0
)
data = np.random.ranf([3, 4, 5]).astype(np.float32)
reduced = np.log(np.sum(data, axis=(0, 1), keepdims=False))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_asc_axes')
```
### ReduceLogSumExp
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceLogSumExp',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims
)
data = np.array(
[[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],
dtype=np.float32)
reduced = np.log(np.sum(np.exp(data),
axis=axes,
keepdims=keepdims == 1))
# print(reduced)
# [[[60.00671387]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_exp_default_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.log(np.sum(np.exp(data),
axis=axes,
keepdims=keepdims == 1))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_exp_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 0
node = onnx.helper.make_node(
'ReduceLogSumExp',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.array(
[[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],
dtype=np.float32)
reduced = np.log(np.sum(
np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))
# print(reduced)
#[[20., 2.31326175]
# [40.00004578, 2.31326175]
# [60.00671387, 2.31326175]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_exp_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.log(np.sum(
np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_exp_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceLogSumExp',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.array(
[[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],
dtype=np.float32)
reduced = np.log(np.sum(np.exp(data),
axis=tuple(axes),
keepdims=keepdims == 1))
# print(reduced)
# [[[20., 2.31326175]]
# [[40.00004578, 2.31326175]]
# [[60.00671387, 2.31326175]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_exp_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.log(np.sum(np.exp(data),
axis=tuple(axes),
keepdims=keepdims == 1))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_exp_keepdims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceLogSumExp',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims
)
data = np.array(
[[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],
dtype=np.float32)
reduced = np.log(np.sum(np.exp(data),
axis=tuple(axes),
keepdims=keepdims == 1))
# print(reduced)
# [[[20., 2.31326175]]
# [[40.00004578, 2.31326175]]
# [[60.00671387, 2.31326175]]]
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_exp_negative_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.log(np.sum(np.exp(data),
axis=tuple(axes),
keepdims=keepdims == 1))
expect(node, inputs=[data], outputs=[reduced],
name='test_reduce_log_sum_exp_negative_axes_keepdims_random')
```
### ReduceMax
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceMax',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)
#print(reduced)
[[[60.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdim_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 0
node = onnx.helper.make_node(
'ReduceMax',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[20., 2.]
# [40., 2.]
# [60., 2.]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceMax',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[[20., 2.]]
# [[40., 2.]]
# [[60., 2.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceMax',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
# print(reduced)
#[[[20., 2.]]
# [[40., 2.]]
# [[60., 2.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_random')
```
### ReduceMean
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceMean',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.mean(data, axis=axes, keepdims=keepdims == 1)
#print(reduced)
#[[[18.25]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_default_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.mean(data, axis=axes, keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 0
node = onnx.helper.make_node(
'ReduceMean',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[12.5, 1.5]
# [35., 1.5]
# [57.5, 1.5]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceMean',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[[12.5, 1.5]]
# [[35., 1.5]]
# [[57.5, 1.5]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_keepdims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceMean',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)
# print(reduced)
# [[[12.5, 1.5]]
# [[35., 1.5]]
# [[57.5, 1.5]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_negative_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_negative_axes_keepdims_random')
```
### ReduceMin
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceMin',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)
#print(reduced)
#[[[1.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 0
node = onnx.helper.make_node(
'ReduceMin',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[5., 1.]
# [30., 1.]
# [55., 1.]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceMin', inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[[5., 1.]]
# [[30., 1.]]
# [[55., 1.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceMin', inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)
reduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
# print(reduced)
#[[[5., 1.]]
# [[30., 1.]]
# [[55., 1.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_random')
```
### ReduceProd
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceProd',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.prod(data, axis=axes, keepdims=keepdims == 1)
#print(reduced)
#[[[4.790016e+08]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_default_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.prod(data, axis=axes, keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 0
node = onnx.helper.make_node(
'ReduceProd',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[3., 8.]
# [35., 48.]
# [99., 120.]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceProd',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[[3., 8.]]
# [[35., 48.]]
# [[99., 120.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_keepdims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceProd',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)
# print(reduced)
#[[[3., 8.]]
# [[35., 48.]]
# [[99., 120.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_negative_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_negative_axes_keepdims_random')
```
### ReduceSum
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceSum',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.sum(data, axis=axes, keepdims=keepdims == 1)
#print(reduced)
#[[[78.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_default_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(data, axis=axes, keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 0
node = onnx.helper.make_node(
'ReduceSum',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.sum(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[4., 6.]
# [12., 14.]
# [20., 22.]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceSum',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.sum(data, axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[[4., 6.]]
# [[12., 14.]]
# [[20., 22.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_keepdims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceSum',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.sum(data, axis=tuple(axes), keepdims=keepdims == 1)
# print(reduced)
#[[[4., 6.]]
# [[12., 14.]]
# [[20., 22.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_negative_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(data, axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_negative_axes_keepdims_random')
```
### ReduceSumSquare
There are 4 test cases, listed as following:
default_axes_keepdims
```python
shape = [3, 2, 2]
axes = None
keepdims = 1
node = onnx.helper.make_node(
'ReduceSumSquare',
inputs=['data'],
outputs=['reduced'],
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.sum(np.square(data), axis=axes, keepdims=keepdims == 1)
#print(reduced)
#[[[650.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_default_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(np.square(data), axis=axes, keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_default_axes_keepdims_random')
```
do_not_keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 0
node = onnx.helper.make_node(
'ReduceSumSquare',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[10., 20.]
# [74., 100.]
# [202., 244.]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_do_not_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_do_not_keepdims_random')
```
keepdims
```python
shape = [3, 2, 2]
axes = [1]
keepdims = 1
node = onnx.helper.make_node(
'ReduceSumSquare',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)
#print(reduced)
#[[[10., 20.]]
# [[74., 100.]]
# [[202., 244.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_keepdims_random')
```
negative_axes_keepdims
```python
shape = [3, 2, 2]
axes = [-2]
keepdims = 1
node = onnx.helper.make_node(
'ReduceSumSquare',
inputs=['data'],
outputs=['reduced'],
axes=axes,
keepdims=keepdims)
data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)
reduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)
# print(reduced)
#[[[10., 20.s]]
# [[74., 100.]]
# [[202., 244.]]]
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_negative_axes_keepdims_example')
np.random.seed(0)
data = np.random.uniform(-10, 10, shape).astype(np.float32)
reduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_negative_axes_keepdims_random')
```
### Relu
There are 1 test cases, listed as following:
relu
```python
node = onnx.helper.make_node(
'Relu',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, 0, np.inf)
expect(node, inputs=[x], outputs=[y],
name='test_relu')
```
### Reshape
There are 1 test cases, listed as following:
reshape
```python
original_shape = [2, 3, 4]
test_cases = {
'reordered_all_dims': np.array([4, 2, 3], dtype=np.int64),
'reordered_last_dims': np.array([2, 4, 3], dtype=np.int64),
'reduced_dims': np.array([2, 12], dtype=np.int64),
'extended_dims': np.array([2, 3, 2, 2], dtype=np.int64),
'one_dim': np.array([24], dtype=np.int64),
'negative_dim': np.array([2, -1, 2], dtype=np.int64),
'negative_extended_dims': np.array([-1, 2, 3, 4], dtype=np.int64),
'zero_dim': np.array([2, 0, 4, 1], dtype=np.int64),
'zero_and_negative_dim': np.array([2, 0, 1, -1], dtype=np.int64),
}
data = np.random.random_sample(original_shape).astype(np.float32)
for test_name, shape in test_cases.items():
node = onnx.helper.make_node(
'Reshape',
inputs=['data', 'shape'],
outputs=['reshaped'],
)
reshaped = reshape_reference_implementation(data, shape)
expect(node, inputs=[data, shape], outputs=[reshaped],
name='test_reshape_' + test_name)
```
### Resize
There are 24 test cases, listed as following:
resize_downsample_scales_cubic
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='cubic',
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)
# [[[[ 1.47119141 2.78125 4.08251953]
# [ 6.71142578 8.02148438 9.32275391]
# [11.91650391 13.2265625 14.52783203]]]]
output = interpolate_nd(
data, cubic_coeffs, scale_factors=scales).astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_downsample_scales_cubic')
```
resize_downsample_scales_cubic_A_n0p5_exclude_outside
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='cubic',
cubic_coeff_a=-0.5,
exclude_outside=True
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)
# [[[[ 1.36812675 2.6695014 4.0133367 ]
# [ 6.57362535 7.875 9.2188353 ]
# [11.94896657 13.25034122 14.59417652]]]]
output = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.5), scale_factors=scales,
exclude_outside=True).astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_downsample_scales_cubic_A_n0p5_exclude_outside')
```
resize_downsample_scales_cubic_align_corners
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='cubic',
coordinate_transformation_mode='align_corners'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)
# [[[[ 1. 2.39519159 3.79038317]
# [ 6.58076634 7.97595793 9.37114951]
# [12.16153268 13.55672427 14.95191585]]]]
output = interpolate_nd(
data, cubic_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_downsample_scales_cubic_align_corners')
```
resize_downsample_scales_linear
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='linear',
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)
# [[[[2.6666665 4.3333331]]]]
output = interpolate_nd(
data, linear_coeffs, scale_factors=scales).astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_downsample_scales_linear')
```
resize_downsample_scales_linear_align_corners
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='linear',
coordinate_transformation_mode='align_corners'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)
# [[[[1. 3.142857]]]]
output = interpolate_nd(
data, linear_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_downsample_scales_linear_align_corners')
```
resize_downsample_scales_nearest
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='nearest',
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)
# [[[[1. 3.]]]]
output = interpolate_nd(
data, nearest_coeffs, scale_factors=scales).astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_downsample_scales_nearest')
```
resize_downsample_sizes_cubic
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='cubic',
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 3, 3], dtype=np.int64)
# [[[[ 1.63078704 3.00462963 4.37847222]
# [ 7.12615741 8.5 9.87384259]
# [12.62152778 13.99537037 15.36921296]]]]
output = interpolate_nd(
data, cubic_coeffs, output_size=sizes).astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_downsample_sizes_cubic')
```
resize_downsample_sizes_linear_pytorch_half_pixel
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='linear',
coordinate_transformation_mode='pytorch_half_pixel'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 3, 1], dtype=np.int64)
# [[[[ 1.6666666]
# [ 7. ]
# [12.333333 ]]]]
output = interpolate_nd(
data, linear_coeffs, output_size=sizes, coordinate_transformation_mode='pytorch_half_pixel').astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_downsample_sizes_linear_pytorch_half_pixel')
```
resize_downsample_sizes_nearest
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='nearest',
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 1, 3], dtype=np.int64)
# [[[[1. 3.]]]]
output = interpolate_nd(
data, nearest_coeffs, output_size=sizes).astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_downsample_sizes_nearest')
```
resize_downsample_sizes_nearest_tf_half_pixel_for_nn
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='nearest',
coordinate_transformation_mode='tf_half_pixel_for_nn'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 3, 2], dtype=np.int64)
# [[[[ 6. 8.]
# [10. 12.]
# [14. 16.]]]]
output = interpolate_nd(
data, nearest_coeffs, output_size=sizes, coordinate_transformation_mode='tf_half_pixel_for_nn').astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_downsample_sizes_nearest_tf_half_pixel_for_nn')
```
resize_tf_crop_and_resize
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='linear',
coordinate_transformation_mode='tf_crop_and_resize'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
# Note: for some rois, the result may be different with that of TF for inaccurate floating point
roi = np.array([0, 0, 0.4, 0.6, 1, 1, 0.6, 0.8], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 3, 3], dtype=np.int64)
# [[[[ 7.6000004 7.9 8.2 ]
# [ 8.8 9.1 9.400001 ]
# [10. 10.3 10.6 ]]]]
output = interpolate_nd(data, linear_coeffs, output_size=sizes, roi=roi,
coordinate_transformation_mode='tf_crop_and_resize').astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_tf_crop_and_resize')
```
resize_tf_crop_and_resize_extrapolation_value
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='linear',
coordinate_transformation_mode='tf_crop_and_resize',
extrapolation_value=10.0
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
# Note: for some rois, the result may be different with that of TF for inaccurate floating point
roi = np.array([0, 0, 0.4, 0.6, 1, 1, 1.2, 1.7], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 3, 3], dtype=np.int64)
# [[[[ 7.6000004 10. 10. ]
# [12.400001 10. 10. ]
# [10. 10. 10. ]]]]
output = interpolate_nd(data, linear_coeffs, output_size=sizes, roi=roi,
coordinate_transformation_mode='tf_crop_and_resize', extrapolation_value=10.0).astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_tf_crop_and_resize')
```
resize_upsample_scales_cubic
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='cubic',
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
# [[[[ 0.47265625 0.76953125 1.24609375 1.875 2.28125
# 2.91015625 3.38671875 3.68359375]
# [ 1.66015625 1.95703125 2.43359375 3.0625 3.46875
# 4.09765625 4.57421875 4.87109375]
# [ 3.56640625 3.86328125 4.33984375 4.96875 5.375
# 6.00390625 6.48046875 6.77734375]
# [ 6.08203125 6.37890625 6.85546875 7.484375 7.890625
# 8.51953125 8.99609375 9.29296875]
# [ 7.70703125 8.00390625 8.48046875 9.109375 9.515625
# 10.14453125 10.62109375 10.91796875]
# [10.22265625 10.51953125 10.99609375 11.625 12.03125
# 12.66015625 13.13671875 13.43359375]
# [12.12890625 12.42578125 12.90234375 13.53125 13.9375
# 14.56640625 15.04296875 15.33984375]
# [13.31640625 13.61328125 14.08984375 14.71875 15.125
# 15.75390625 16.23046875 16.52734375]]]]
output = interpolate_nd(
data, cubic_coeffs, scale_factors=scales).astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_upsample_scales_cubic')
```
resize_upsample_scales_cubic_A_n0p5_exclude_outside
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='cubic',
cubic_coeff_a=-0.5,
exclude_outside=True
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
# [[[[ 0.55882353 0.81494204 1.35698249 1.89705882 2.39705882
# 2.93713516 3.47917561 3.73529412]
# [ 1.58329755 1.83941606 2.38145651 2.92153285 3.42153285
# 3.96160918 4.50364964 4.75976814]
# [ 3.75145936 4.00757787 4.54961832 5.08969466 5.58969466
# 6.12977099 6.67181144 6.92792995]
# [ 5.91176471 6.16788321 6.70992366 7.25 7.75
# 8.29007634 8.83211679 9.08823529]
# [ 7.91176471 8.16788321 8.70992366 9.25 9.75
# 10.29007634 10.83211679 11.08823529]
# [10.07207005 10.32818856 10.87022901 11.41030534 11.91030534
# 12.45038168 12.99242213 13.24854064]
# [12.24023186 12.49635036 13.03839082 13.57846715 14.07846715
# 14.61854349 15.16058394 15.41670245]
# [13.26470588 13.52082439 14.06286484 14.60294118 15.10294118
# 15.64301751 16.18505796 16.44117647]]]]
output = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.5), scale_factors=scales,
exclude_outside=True).astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_upsample_scales_cubic_A_n0p5_exclude_outside')
```
resize_upsample_scales_cubic_align_corners
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='cubic',
coordinate_transformation_mode='align_corners'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
# [[[[ 1. 1.34110787 1.80029155 2.32944606 2.67055394
# 3.19970845 3.65889213 4. ]
# [ 2.36443149 2.70553936 3.16472303 3.69387755 4.03498542
# 4.56413994 5.02332362 5.36443149]
# [ 4.20116618 4.54227405 5.00145773 5.53061224 5.87172012
# 6.40087464 6.86005831 7.20116618]
# [ 6.31778426 6.65889213 7.1180758 7.64723032 7.98833819
# 8.51749271 8.97667638 9.31778426]
# [ 7.68221574 8.02332362 8.48250729 9.01166181 9.35276968
# 9.8819242 10.34110787 10.68221574]
# [ 9.79883382 10.13994169 10.59912536 11.12827988 11.46938776
# 11.99854227 12.45772595 12.79883382]
# [11.63556851 11.97667638 12.43586006 12.96501458 13.30612245
# 13.83527697 14.29446064 14.63556851]
# [13. 13.34110787 13.80029155 14.32944606 14.67055394
# 15.19970845 15.65889213 16. ]]]]
output = interpolate_nd(
data, cubic_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_upsample_scales_cubic_align_corners')
```
resize_upsample_scales_cubic_asymmetric
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='cubic',
coordinate_transformation_mode='asymmetric'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
roi = np.array([], dtype=np.float32)
# [[[[ 1. 1.40625 2. 2.5 3. 3.59375 4.
# 4.09375]
# [ 2.625 3.03125 3.625 4.125 4.625 5.21875 5.625
# 5.71875]
# [ 5. 5.40625 6. 6.5 7. 7.59375 8.
# 8.09375]
# [ 7. 7.40625 8. 8.5 9. 9.59375 10.
# 10.09375]
# [ 9. 9.40625 10. 10.5 11. 11.59375 12.
# 12.09375]
# [11.375 11.78125 12.375 12.875 13.375 13.96875 14.375
# 14.46875]
# [13. 13.40625 14. 14.5 15. 15.59375 16.
# 16.09375]
# [13.375 13.78125 14.375 14.875 15.375 15.96875 16.375
# 16.46875]]]]
output = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.75), scale_factors=scales,
coordinate_transformation_mode='asymmetric').astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_upsample_scales_cubic_asymmetric')
```
resize_upsample_scales_linear
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='linear',
)
data = np.array([[[
[1, 2],
[3, 4],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
# [[[[1. 1.25 1.75 2. ]
# [1.5 1.75 2.25 2.5 ]
# [2.5 2.75 3.25 3.5 ]
# [3. 3.25 3.75 4. ]]]]
output = interpolate_nd(
data, linear_coeffs, scale_factors=scales).astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_upsample_scales_linear')
```
resize_upsample_scales_linear_align_corners
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='linear',
coordinate_transformation_mode='align_corners'
)
data = np.array([[[
[1, 2],
[3, 4],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
# [[[[1. 1.33333333 1.66666667 2. ]
# [1.66666667 2. 2.33333333 2.66666667]
# [2.33333333 2.66666667 3. 3.33333333]
# [3. 3.33333333 3.66666667 4. ]]]]
output = interpolate_nd(
data, linear_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_upsample_scales_linear_align_corners')
```
resize_upsample_scales_nearest
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales'],
outputs=['Y'],
mode='nearest',
)
data = np.array([[[
[1, 2],
[3, 4],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)
# [[[[1. 1. 1. 2. 2. 2.]
# [1. 1. 1. 2. 2. 2.]
# [3. 3. 3. 4. 4. 4.]
# [3. 3. 3. 4. 4. 4.]]]]
output = interpolate_nd(
data, nearest_coeffs, scale_factors=scales).astype(np.float32)
expect(node, inputs=[data, roi, scales], outputs=[output],
name='test_resize_upsample_scales_nearest')
```
resize_upsample_sizes_cubic
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='cubic',
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 9, 10], dtype=np.int64)
# [[[[ 0.45507922 0.64057922 0.97157922 1.42257922 1.90732922
# 2.22332922 2.70807922 3.15907922 3.49007922 3.67557922]
# [ 1.39437963 1.57987963 1.91087963 2.36187963 2.84662963
# 3.16262963 3.64737963 4.09837963 4.42937963 4.61487963]
# [ 2.95130693 3.13680693 3.46780693 3.91880693 4.40355693
# 4.71955693 5.20430693 5.65530693 5.98630693 6.17180693]
# [ 5.20525069 5.39075069 5.72175069 6.17275069 6.65750069
# 6.97350069 7.45825069 7.90925069 8.24025069 8.42575069]
# [ 6.88975 7.07525 7.40625 7.85725 8.342
# 8.658 9.14275 9.59375 9.92475 10.11025 ]
# [ 8.57424931 8.75974931 9.09074931 9.54174931 10.02649931
# 10.34249931 10.82724931 11.27824931 11.60924931 11.79474931]
# [10.82819307 11.01369307 11.34469307 11.79569307 12.28044307
# 12.59644307 13.08119307 13.53219307 13.86319307 14.04869307]
# [12.38512037 12.57062037 12.90162037 13.35262037 13.83737037
# 14.15337037 14.63812037 15.08912037 15.42012037 15.60562037]
# [13.32442078 13.50992078 13.84092078 14.29192078 14.77667078
# 15.09267078 15.57742078 16.02842078 16.35942078 16.54492078]]]]
output = interpolate_nd(
data, cubic_coeffs, output_size=sizes).astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_upsample_sizes_cubic')
```
resize_upsample_sizes_nearest
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='nearest',
)
data = np.array([[[
[1, 2],
[3, 4],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 7, 8], dtype=np.int64)
# [[[[1. 1. 1. 1. 2. 2. 2. 2.]
# [1. 1. 1. 1. 2. 2. 2. 2.]
# [1. 1. 1. 1. 2. 2. 2. 2.]
# [1. 1. 1. 1. 2. 2. 2. 2.]
# [3. 3. 3. 3. 4. 4. 4. 4.]
# [3. 3. 3. 3. 4. 4. 4. 4.]
# [3. 3. 3. 3. 4. 4. 4. 4.]]]]
output = interpolate_nd(
data, nearest_coeffs, output_size=sizes).astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_upsample_sizes_nearest')
```
resize_upsample_sizes_nearest_ceil_half_pixel
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='nearest',
coordinate_transformation_mode='half_pixel'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 8, 8], dtype=np.int64)
# [[[[ 1. 2. 2. 3. 3. 4. 4. 4.]
# [ 5. 6. 6. 7. 7. 8. 8. 8.]
# [ 5. 6. 6. 7. 7. 8. 8. 8.]
# [ 9. 10. 10. 11. 11. 12. 12. 12.]
# [ 9. 10. 10. 11. 11. 12. 12. 12.]
# [13. 14. 14. 15. 15. 16. 16. 16.]
# [13. 14. 14. 15. 15. 16. 16. 16.]
# [13. 14. 14. 15. 15. 16. 16. 16.]]]]
output = interpolate_nd(
data, lambda x: nearest_coeffs(x, mode='ceil'), output_size=sizes).astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_upsample_sizes_nearest_ceil_half_pixel')
```
resize_upsample_sizes_nearest_floor_align_corners
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='nearest',
coordinate_transformation_mode='align_corners'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 8, 8], dtype=np.int64)
# [[[[ 1. 1. 1. 2. 2. 3. 3. 4.]
# [ 1. 1. 1. 2. 2. 3. 3. 4.]
# [ 1. 1. 1. 2. 2. 3. 3. 4.]
# [ 5. 5. 5. 6. 6. 7. 7. 8.]
# [ 5. 5. 5. 6. 6. 7. 7. 8.]
# [ 9. 9. 9. 10. 10. 11. 11. 12.]
# [ 9. 9. 9. 10. 10. 11. 11. 12.]
# [13. 13. 13. 14. 14. 15. 15. 16.]]]]
output = interpolate_nd(
data, lambda x: nearest_coeffs(x, mode='floor'), output_size=sizes, coordinate_transformation_mode='align_corners').astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_upsample_sizes_nearest_floor_align_corners')
```
resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric
```python
node = onnx.helper.make_node(
'Resize',
inputs=['X', 'roi', 'scales', 'sizes'],
outputs=['Y'],
mode='nearest',
coordinate_transformation_mode='asymmetric'
)
data = np.array([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]]], dtype=np.float32)
roi = np.array([], dtype=np.float32)
scales = np.array([], dtype=np.float32)
sizes = np.array([1, 1, 8, 8], dtype=np.int64)
# [[[[ 1. 2. 2. 3. 3. 4. 4. 4.]
# [ 5. 6. 6. 7. 7. 8. 8. 8.]
# [ 5. 6. 6. 7. 7. 8. 8. 8.]
# [ 9. 10. 10. 11. 11. 12. 12. 12.]
# [ 9. 10. 10. 11. 11. 12. 12. 12.]
# [13. 14. 14. 15. 15. 16. 16. 16.]
# [13. 14. 14. 15. 15. 16. 16. 16.]
# [13. 14. 14. 15. 15. 16. 16. 16.]]]]
output = interpolate_nd(
data, lambda x: nearest_coeffs(x, mode='round_prefer_ceil'),
output_size=sizes, coordinate_transformation_mode='asymmetric').astype(np.float32)
expect(node, inputs=[data, roi, scales, sizes], outputs=[output],
name='test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric')
```
### ReverseSequence
There are 2 test cases, listed as following:
reversesequence_batch
```python
node = onnx.helper.make_node(
'ReverseSequence',
inputs=['x', 'sequence_lens'],
outputs=['y'],
time_axis=1,
batch_axis=0,
)
x = np.array([[0.0, 1.0, 2.0, 3.0],
[4.0, 5.0, 6.0, 7.0],
[8.0, 9.0, 10.0, 11.0],
[12.0, 13.0, 14.0, 15.0]], dtype=np.float32)
sequence_lens = np.array([1, 2, 3, 4], dtype=np.int64)
y = np.array([[0.0, 1.0, 2.0, 3.0],
[5.0, 4.0, 6.0, 7.0],
[10.0, 9.0, 8.0, 11.0],
[15.0, 14.0, 13.0, 12.0]], dtype=np.float32)
expect(node, inputs=[x, sequence_lens], outputs=[y],
name='test_reversesequence_batch')
```
reversesequence_time
```python
node = onnx.helper.make_node(
'ReverseSequence',
inputs=['x', 'sequence_lens'],
outputs=['y'],
time_axis=0,
batch_axis=1,
)
x = np.array([[0.0, 4.0, 8.0, 12.0],
[1.0, 5.0, 9.0, 13.0],
[2.0, 6.0, 10.0, 14.0],
[3.0, 7.0, 11.0, 15.0]], dtype=np.float32)
sequence_lens = np.array([4, 3, 2, 1], dtype=np.int64)
y = np.array([[3.0, 6.0, 9.0, 12.0],
[2.0, 5.0, 8.0, 13.0],
[1.0, 4.0, 10.0, 14.0],
[0.0, 7.0, 11.0, 15.0]], dtype=np.float32)
expect(node, inputs=[x, sequence_lens], outputs=[y],
name='test_reversesequence_time')
```
### RoiAlign
There are 1 test cases, listed as following:
roialign
```python
node = onnx.helper.make_node(
"RoiAlign",
inputs=["X", "rois", "batch_indices"],
outputs=["Y"],
spatial_scale=1.0,
output_height=5,
output_width=5,
sampling_ratio=2,
)
X = np.array(
[
[
[
[
0.2764,
0.7150,
0.1958,
0.3416,
0.4638,
0.0259,
0.2963,
0.6518,
0.4856,
0.7250,
],
[
0.9637,
0.0895,
0.2919,
0.6753,
0.0234,
0.6132,
0.8085,
0.5324,
0.8992,
0.4467,
],
[
0.3265,
0.8479,
0.9698,
0.2471,
0.9336,
0.1878,
0.4766,
0.4308,
0.3400,
0.2162,
],
[
0.0206,
0.1720,
0.2155,
0.4394,
0.0653,
0.3406,
0.7724,
0.3921,
0.2541,
0.5799,
],
[
0.4062,
0.2194,
0.4473,
0.4687,
0.7109,
0.9327,
0.9815,
0.6320,
0.1728,
0.6119,
],
[
0.3097,
0.1283,
0.4984,
0.5068,
0.4279,
0.0173,
0.4388,
0.0430,
0.4671,
0.7119,
],
[
0.1011,
0.8477,
0.4726,
0.1777,
0.9923,
0.4042,
0.1869,
0.7795,
0.9946,
0.9689,
],
[
0.1366,
0.3671,
0.7011,
0.6234,
0.9867,
0.5585,
0.6985,
0.5609,
0.8788,
0.9928,
],
[
0.5697,
0.8511,
0.6711,
0.9406,
0.8751,
0.7496,
0.1650,
0.1049,
0.1559,
0.2514,
],
[
0.7012,
0.4056,
0.7879,
0.3461,
0.0415,
0.2998,
0.5094,
0.3727,
0.5482,
0.0502,
],
]
]
],
dtype=np.float32,
)
batch_indices = np.array([0, 0, 0], dtype=np.int64)
rois = np.array([[0, 0, 9, 9], [0, 5, 4, 9], [5, 5, 9, 9]], dtype=np.float32)
# (num_rois, C, output_height, output_width)
Y = np.array(
[
[
[
[0.4664, 0.4466, 0.3405, 0.5688, 0.6068],
[0.3714, 0.4296, 0.3835, 0.5562, 0.3510],
[0.2768, 0.4883, 0.5222, 0.5528, 0.4171],
[0.4713, 0.4844, 0.6904, 0.4920, 0.8774],
[0.6239, 0.7125, 0.6289, 0.3355, 0.3495],
]
],
[
[
[0.3022, 0.4305, 0.4696, 0.3978, 0.5423],
[0.3656, 0.7050, 0.5165, 0.3172, 0.7015],
[0.2912, 0.5059, 0.6476, 0.6235, 0.8299],
[0.5916, 0.7389, 0.7048, 0.8372, 0.8893],
[0.6227, 0.6153, 0.7097, 0.6154, 0.4585],
]
],
[
[
[0.2384, 0.3379, 0.3717, 0.6100, 0.7601],
[0.3767, 0.3785, 0.7147, 0.9243, 0.9727],
[0.5749, 0.5826, 0.5709, 0.7619, 0.8770],
[0.5355, 0.2566, 0.2141, 0.2796, 0.3600],
[0.4365, 0.3504, 0.2887, 0.3661, 0.2349],
]
],
],
dtype=np.float32,
)
expect(node, inputs=[X, rois, batch_indices], outputs=[Y], name="test_roialign")
```
### Round
There are 1 test cases, listed as following:
round
```python
node = onnx.helper.make_node(
'Round',
inputs=['x'],
outputs=['y'],
)
x = np.array([0.1, 0.5, 0.9, 1.2, 1.5,
1.8, 2.3, 2.5, 2.7, -1.1,
-1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float32)
y = np.array([0., 0., 1., 1., 2.,
2., 2., 2., 3., -1.,
-2., -2., -2., -2., -3.]).astype(np.float32) # expected output
expect(node, inputs=[x], outputs=[y],
name='test_round')
```
### Scan
There are 2 test cases, listed as following:
scan_8
```python
# Given an input sequence [x1, ..., xN], sum up its elements using a scan
# returning the final state (x1+x2+...+xN) as well the scan_output
# [x1, x1+x2, ..., x1+x2+...+xN]
#
# create graph to represent scan body
sum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])
next = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])
sum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])
scan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])
add_node = onnx.helper.make_node(
'Add',
inputs=['sum_in', 'next'],
outputs=['sum_out']
)
id_node = onnx.helper.make_node(
'Identity',
inputs=['sum_out'],
outputs=['scan_out']
)
scan_body = onnx.helper.make_graph(
[add_node, id_node],
'scan_body',
[sum_in, next],
[sum_out, scan_out]
)
# create scan op node
no_sequence_lens = '' # optional input, not supplied
node = onnx.helper.make_node(
'Scan',
inputs=[no_sequence_lens, 'initial', 'x'],
outputs=['y', 'z'],
num_scan_inputs=1,
body=scan_body
)
# create inputs for batch-size 1, sequence-length 3, inner dimension 2
initial = np.array([0, 0]).astype(np.float32).reshape((1, 2))
x = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((1, 3, 2))
# final state computed = [1 + 3 + 5, 2 + 4 + 6]
y = np.array([9, 12]).astype(np.float32).reshape((1, 2))
# scan-output computed
z = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((1, 3, 2))
expect(node, inputs=[initial, x], outputs=[y, z],
name='test_scan_sum', opset_imports=[onnx.helper.make_opsetid("", 8)])
```
scan_9
```python
# Given an input sequence [x1, ..., xN], sum up its elements using a scan
# returning the final state (x1+x2+...+xN) as well the scan_output
# [x1, x1+x2, ..., x1+x2+...+xN]
#
# create graph to represent scan body
sum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])
next = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])
sum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])
scan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])
add_node = onnx.helper.make_node(
'Add',
inputs=['sum_in', 'next'],
outputs=['sum_out']
)
id_node = onnx.helper.make_node(
'Identity',
inputs=['sum_out'],
outputs=['scan_out']
)
scan_body = onnx.helper.make_graph(
[add_node, id_node],
'scan_body',
[sum_in, next],
[sum_out, scan_out]
)
# create scan op node
node = onnx.helper.make_node(
'Scan',
inputs=['initial', 'x'],
outputs=['y', 'z'],
num_scan_inputs=1,
body=scan_body
)
# create inputs for sequence-length 3, inner dimension 2
initial = np.array([0, 0]).astype(np.float32).reshape((2,))
x = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))
# final state computed = [1 + 3 + 5, 2 + 4 + 6]
y = np.array([9, 12]).astype(np.float32).reshape((2,))
# scan-output computed
z = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))
expect(node, inputs=[initial, x], outputs=[y, z],
name='test_scan9_sum', opset_imports=[onnx.helper.make_opsetid("", 9)])
```
### Scatter
There are 2 test cases, listed as following:
scatter_with_axis
```python
axis = 1
node = onnx.helper.make_node(
'Scatter',
inputs=['data', 'indices', 'updates'],
outputs=['y'],
axis=axis,
)
data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
indices = np.array([[1, 3]], dtype=np.int64)
updates = np.array([[1.1, 2.1]], dtype=np.float32)
y = scatter(data, indices, updates, axis=axis)
# print(y) produces
# [[1.0, 1.1, 3.0, 2.1, 5.0]]
expect(node, inputs=[data, indices, updates], outputs=[y],
name='test_scatter_with_axis', opset_imports=[helper.make_opsetid("", 10)])
```
scatter_without_axis
```python
node = onnx.helper.make_node(
'Scatter',
inputs=['data', 'indices', 'updates'],
outputs=['y'],
)
data = np.zeros((3, 3), dtype=np.float32)
indices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)
updates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)
y = scatter(data, indices, updates)
# print(y) produces
# [[2.0, 1.1, 0.0],
# [1.0, 0.0, 2.2],
# [0.0, 2.1, 1.2]]
expect(node, inputs=[data, indices, updates], outputs=[y],
name='test_scatter_without_axis', opset_imports=[helper.make_opsetid("", 10)])
```
### ScatterElements
There are 3 test cases, listed as following:
scatter_elements_with_axis
```python
axis = 1
node = onnx.helper.make_node(
'ScatterElements',
inputs=['data', 'indices', 'updates'],
outputs=['y'],
axis=axis,
)
data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
indices = np.array([[1, 3]], dtype=np.int64)
updates = np.array([[1.1, 2.1]], dtype=np.float32)
y = scatter_elements(data, indices, updates, axis)
# print(y) produces
# [[1.0, 1.1, 3.0, 2.1, 5.0]]
expect(node, inputs=[data, indices, updates], outputs=[y],
name='test_scatter_elements_with_axis')
```
scatter_elements_with_negative_indices
```python
axis = 1
node = onnx.helper.make_node(
'ScatterElements',
inputs=['data', 'indices', 'updates'],
outputs=['y'],
axis=axis,
)
data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
indices = np.array([[1, -3]], dtype=np.int64)
updates = np.array([[1.1, 2.1]], dtype=np.float32)
y = scatter_elements(data, indices, updates, axis)
# print(y) produces
# [[1.0, 1.1, 2.1, 4.0, 5.0]]
expect(node, inputs=[data, indices, updates], outputs=[y],
name='test_scatter_elements_with_negative_indices')
```
scatter_elements_without_axis
```python
node = onnx.helper.make_node(
'ScatterElements',
inputs=['data', 'indices', 'updates'],
outputs=['y'],
)
data = np.zeros((3, 3), dtype=np.float32)
indices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)
updates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)
y = scatter_elements(data, indices, updates)
# print(y) produces
# [[2.0, 1.1, 0.0],
# [1.0, 0.0, 2.2],
# [0.0, 2.1, 1.2]]
expect(node, inputs=[data, indices, updates], outputs=[y],
name='test_scatter_elements_without_axis')
```
### ScatterND
There are 1 test cases, listed as following:
scatternd
```python
node = onnx.helper.make_node(
'ScatterND',
inputs=['data', 'indices', 'updates'],
outputs=['y'],
)
data = np.array(
[[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
[[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],
[[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)
indices = np.array([[0], [2]], dtype=np.int64)
updates = np.array(
[[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)
# Expecting output as np.array(
# [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],
# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)
output = scatter_nd_impl(data, indices, updates)
expect(node, inputs=[data, indices, updates], outputs=[output],
name='test_scatternd')
```
### Selu
There are 2 test cases, listed as following:
selu
```python
node = onnx.helper.make_node(
'Selu',
inputs=['x'],
outputs=['y'],
alpha=2.0,
gamma=3.0
)
x = np.array([-1, 0, 1]).astype(np.float32)
# expected output [-3.79272318, 0., 3.]
y = np.clip(x, 0, np.inf) * 3.0 + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0
expect(node, inputs=[x], outputs=[y],
name='test_selu_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, 0, np.inf) * 3.0 + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0
expect(node, inputs=[x], outputs=[y],
name='test_selu')
```
selu_default
```python
default_alpha = 1.67326319217681884765625
default_gamma = 1.05070102214813232421875
node = onnx.helper.make_node(
'Selu',
inputs=['x'],
outputs=['y'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, 0, np.inf) * default_gamma + \
(np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha * default_gamma
expect(node, inputs=[x], outputs=[y],
name='test_selu_default')
```
### Shape
There are 1 test cases, listed as following:
shape
```python
node = onnx.helper.make_node(
'Shape',
inputs=['x'],
outputs=['y'],
)
x = np.array([
[1, 2, 3],
[4, 5, 6],
]).astype(np.float32)
y = np.array([
2, 3,
]).astype(np.int64)
expect(node, inputs=[x], outputs=[y],
name='test_shape_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.array(x.shape).astype(np.int64)
expect(node, inputs=[x], outputs=[y],
name='test_shape')
```
### Shrink
There are 2 test cases, listed as following:
hard_shrink
```python
node = onnx.helper.make_node(
'Shrink',
inputs=['x'],
outputs=['y'],
lambd=1.5,
)
X = np.arange(-2.0, 2.1, dtype=np.float32)
Y = np.array([-2, 0, 0, 0, 2], dtype=np.float32)
expect(node, inputs=[X], outputs=[Y],
name='test_shrink_hard')
```
soft_shrink
```python
node = onnx.helper.make_node(
'Shrink',
inputs=['x'],
outputs=['y'],
lambd=1.5,
bias=1.5,
)
X = np.arange(-2.0, 2.1, dtype=np.float32)
Y = np.array([-0.5, 0, 0, 0, 0.5], dtype=np.float32)
expect(node, inputs=[X], outputs=[Y],
name='test_shrink_soft')
```
### Sigmoid
There are 1 test cases, listed as following:
sigmoid
```python
node = onnx.helper.make_node(
'Sigmoid',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = 1.0 / (1.0 + np.exp(np.negative(x))) # expected output [0.26894143, 0.5, 0.7310586]
expect(node, inputs=[x], outputs=[y],
name='test_sigmoid_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = 1.0 / (1.0 + np.exp(np.negative(x)))
expect(node, inputs=[x], outputs=[y],
name='test_sigmoid')
```
### Sign
There are 1 test cases, listed as following:
sign
```python
node = onnx.helper.make_node(
'Sign',
inputs=['x'],
outputs=['y'],
)
x = np.array(range(-5, 6)).astype(np.float32)
y = np.sign(x)
expect(node, inputs=[x], outputs=[y],
name='test_sign')
```
### Sin
There are 1 test cases, listed as following:
sin
```python
node = onnx.helper.make_node(
'Sin',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.sin(x)
expect(node, inputs=[x], outputs=[y],
name='test_sin_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.sin(x)
expect(node, inputs=[x], outputs=[y],
name='test_sin')
```
### Sinh
There are 1 test cases, listed as following:
sinh
```python
node = onnx.helper.make_node(
'Sinh',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.sinh(x) # expected output [-1.17520118, 0., 1.17520118]
expect(node, inputs=[x], outputs=[y],
name='test_sinh_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.sinh(x)
expect(node, inputs=[x], outputs=[y],
name='test_sinh')
```
### Size
There are 1 test cases, listed as following:
size
```python
node = onnx.helper.make_node(
'Size',
inputs=['x'],
outputs=['y'],
)
x = np.array([
[1, 2, 3],
[4, 5, 6],
]).astype(np.float32)
y = np.array(6).astype(np.int64)
expect(node, inputs=[x], outputs=[y],
name='test_size_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.array(x.size).astype(np.int64)
expect(node, inputs=[x], outputs=[y],
name='test_size')
```
### Slice
There are 8 test cases, listed as following:
slice
```python
node = onnx.helper.make_node(
'Slice',
inputs=['x', 'starts', 'ends', 'axes', 'steps'],
outputs=['y'],
)
x = np.random.randn(20, 10, 5).astype(np.float32)
y = x[0:3, 0:10]
starts = np.array([0, 0], dtype=np.int64)
ends = np.array([3, 10], dtype=np.int64)
axes = np.array([0, 1], dtype=np.int64)
steps = np.array([1, 1], dtype=np.int64)
expect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],
name='test_slice')
```
slice_default_axes
```python
node = onnx.helper.make_node(
'Slice',
inputs=['x', 'starts', 'ends'],
outputs=['y'],
)
x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([0, 0, 3], dtype=np.int64)
ends = np.array([20, 10, 4], dtype=np.int64)
y = x[:, :, 3:4]
expect(node, inputs=[x, starts, ends], outputs=[y],
name='test_slice_default_axes')
```
slice_default_steps
```python
node = onnx.helper.make_node(
'Slice',
inputs=['x', 'starts', 'ends', 'axes'],
outputs=['y'],
)
x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([0, 0, 3], dtype=np.int64)
ends = np.array([20, 10, 4], dtype=np.int64)
axes = np.array([0, 1, 2], dtype=np.int64)
y = x[:, :, 3:4]
expect(node, inputs=[x, starts, ends, axes], outputs=[y],
name='test_slice_default_steps')
```
slice_end_out_of_bounds
```python
node = onnx.helper.make_node(
'Slice',
inputs=['x', 'starts', 'ends', 'axes', 'steps'],
outputs=['y'],
)
x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([1], dtype=np.int64)
ends = np.array([1000], dtype=np.int64)
axes = np.array([1], dtype=np.int64)
steps = np.array([1], dtype=np.int64)
y = x[:, 1:1000]
expect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],
name='test_slice_end_out_of_bounds')
```
slice_neg
```python
node = onnx.helper.make_node(
'Slice',
inputs=['x', 'starts', 'ends', 'axes', 'steps'],
outputs=['y'],
)
x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([0], dtype=np.int64)
ends = np.array([-1], dtype=np.int64)
axes = np.array([1], dtype=np.int64)
steps = np.array([1], dtype=np.int64)
y = x[:, 0:-1]
expect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],
name='test_slice_neg')
```
slice_neg_steps
```python
node = onnx.helper.make_node(
'Slice',
inputs=['x', 'starts', 'ends', 'axes', 'steps'],
outputs=['y'],
)
x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([20, 10, 4], dtype=np.int64)
ends = np.array([0, 0, 1], dtype=np.int64)
axes = np.array([0, 1, 2], dtype=np.int64)
steps = np.array([-1, -3, -2])
y = x[20:0:-1, 10:0:-3, 4:1:-2]
expect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],
name='test_slice_neg_steps')
```
slice_negative_axes
```python
node = onnx.helper.make_node(
'Slice',
inputs=['x', 'starts', 'ends', 'axes'],
outputs=['y'],
)
x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([0, 0, 3], dtype=np.int64)
ends = np.array([20, 10, 4], dtype=np.int64)
axes = np.array([0, -2, -1], dtype=np.int64)
y = x[:, :, 3:4]
expect(node, inputs=[x, starts, ends, axes], outputs=[y],
name='test_slice_negative_axes')
```
slice_start_out_of_bounds
```python
node = onnx.helper.make_node(
'Slice',
inputs=['x', 'starts', 'ends', 'axes', 'steps'],
outputs=['y'],
)
x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([1000], dtype=np.int64)
ends = np.array([1000], dtype=np.int64)
axes = np.array([1], dtype=np.int64)
steps = np.array([1], dtype=np.int64)
y = x[:, 1000:1000]
expect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],
name='test_slice_start_out_of_bounds')
```
### Softmax
There are 2 test cases, listed as following:
softmax
```python
node = onnx.helper.make_node(
'Softmax',
inputs=['x'],
outputs=['y'],
)
x = np.array([[-1, 0, 1]]).astype(np.float32)
# expected output [[0.09003058, 0.24472848, 0.66524094]]
y = np.exp(x) / np.sum(np.exp(x), axis=1)
expect(node, inputs=[x], outputs=[y],
name='test_softmax_example')
```
softmax_axis
```python
def softmax_2d(x): # type: (np.ndarray) -> np.ndarray
max_x = np.max(x, axis=1).reshape((-1, 1))
exp_x = np.exp(x - max_x)
return exp_x / np.sum(exp_x, axis=1).reshape((-1, 1))
x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]).astype(np.float32)
# expected output [[0.0320586, 0.08714432, 0.23688284, 0.64391428],
# [0.0320586, 0.08714432, 0.23688284, 0.64391428]]
y = softmax_2d(x)
node = onnx.helper.make_node(
'Softmax',
inputs=['x'],
outputs=['y'],
)
expect(node, inputs=[x], outputs=[y],
name='test_softmax_large_number')
x = np.abs(np.random.randn(3, 4, 5).astype(np.float32))
node = onnx.helper.make_node(
'Softmax',
inputs=['x'],
outputs=['y'],
axis=0,
)
y = softmax_2d(x.reshape(1, 60)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_softmax_axis_0')
node = onnx.helper.make_node(
'Softmax',
inputs=['x'],
outputs=['y'],
axis=1,
)
y = softmax_2d(x.reshape(3, 20)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_softmax_axis_1')
# default axis is 1
node = onnx.helper.make_node(
'Softmax',
inputs=['x'],
outputs=['y'],
)
expect(node, inputs=[x], outputs=[y],
name='test_softmax_default_axis')
node = onnx.helper.make_node(
'Softmax',
inputs=['x'],
outputs=['y'],
axis=2,
)
y = softmax_2d(x.reshape(12, 5)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_softmax_axis_2')
node = onnx.helper.make_node(
'Softmax',
inputs=['x'],
outputs=['y'],
axis=-1,
)
y = softmax_2d(x.reshape(12, 5)).reshape(3, 4, 5)
expect(node, inputs=[x], outputs=[y],
name='test_softmax_negative_axis')
```
### Softplus
There are 1 test cases, listed as following:
softplus
```python
node = onnx.helper.make_node(
'Softplus',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.log(np.exp(x) + 1) # expected output [0.31326166, 0.69314718, 1.31326163]
expect(node, inputs=[x], outputs=[y],
name='test_softplus_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.log(np.exp(x) + 1)
expect(node, inputs=[x], outputs=[y],
name='test_softplus')
```
### Softsign
There are 1 test cases, listed as following:
softsign
```python
node = onnx.helper.make_node(
'Softsign',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.array([-0.5, 0, 0.5]).astype(np.float32)
expect(node, inputs=[x], outputs=[y],
name='test_softsign_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = x / (1 + np.abs(x))
expect(node, inputs=[x], outputs=[y],
name='test_softsign')
```
### Split
There are 3 test cases, listed as following:
1d
```python
input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)
node = onnx.helper.make_node(
'Split',
inputs=['input'],
outputs=['output_1', 'output_2', 'output_3'],
axis=0
)
expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]
expect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_1d')
node = onnx.helper.make_node(
'Split',
inputs=['input'],
outputs=['output_1', 'output_2'],
axis=0,
split=[2, 4]
)
expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]
expect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_variable_parts_1d')
```
2d
```python
input = np.array([[1., 2., 3., 4., 5., 6.],
[7., 8., 9., 10., 11., 12.]]).astype(np.float32)
node = onnx.helper.make_node(
'Split',
inputs=['input'],
outputs=['output_1', 'output_2'],
axis=1
)
expected_outputs = [np.array([[1., 2., 3.], [7., 8., 9.]]).astype(np.float32),
np.array([[4., 5., 6.], [10., 11., 12.]]).astype(np.float32)]
expect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_2d')
node = onnx.helper.make_node(
'Split',
inputs=['input'],
outputs=['output_1', 'output_2'],
axis=1,
split=[2, 4]
)
expected_outputs = [np.array([[1., 2.], [7., 8.]]).astype(np.float32),
np.array([[3., 4., 5., 6.], [9., 10., 11., 12.]]).astype(np.float32)]
expect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_variable_parts_2d')
```
default_values
```python
input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)
# If axis is not specified, split is applied on default axis 0
node = onnx.helper.make_node(
'Split',
inputs=['input'],
outputs=['output_1', 'output_2', 'output_3']
)
expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]
expect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_default_axis')
node = onnx.helper.make_node(
'Split',
inputs=['input'],
outputs=['output_1', 'output_2'],
split=[2, 4]
)
expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]
expect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_variable_parts_default_axis')
```
### Sqrt
There are 1 test cases, listed as following:
sqrt
```python
node = onnx.helper.make_node(
'Sqrt',
inputs=['x'],
outputs=['y'],
)
x = np.array([1, 4, 9]).astype(np.float32)
y = np.sqrt(x) # expected output [1., 2., 3.]
expect(node, inputs=[x], outputs=[y],
name='test_sqrt_example')
x = np.abs(np.random.randn(3, 4, 5).astype(np.float32))
y = np.sqrt(x)
expect(node, inputs=[x], outputs=[y],
name='test_sqrt')
```
### Squeeze
There are 2 test cases, listed as following:
squeeze
```python
node = onnx.helper.make_node(
'Squeeze',
inputs=['x'],
outputs=['y'],
axes=[0],
)
x = np.random.randn(1, 3, 4, 5).astype(np.float32)
y = np.squeeze(x, axis=0)
expect(node, inputs=[x], outputs=[y],
name='test_squeeze')
```
squeeze_negative_axes
```python
node = onnx.helper.make_node(
'Squeeze',
inputs=['x'],
outputs=['y'],
axes=[-2],
)
x = np.random.randn(1, 3, 1, 5).astype(np.float32)
y = np.squeeze(x, axis=-2)
expect(node, inputs=[x], outputs=[y],
name='test_squeeze_negative_axes')
```
### StringNormalizer
There are 6 test cases, listed as following:
monday_casesensintive_lower
```python
input = np.array([u'monday', u'tuesday', u'wednesday', u'thursday']).astype(np.object)
output = np.array([u'tuesday', u'wednesday', u'thursday']).astype(np.object)
stopwords = [u'monday']
node = onnx.helper.make_node(
'StringNormalizer',
inputs=['x'],
outputs=['y'],
case_change_action='LOWER',
is_case_sensitive=1,
stopwords=stopwords
)
expect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_casesensintive_lower')
```
monday_casesensintive_nochangecase
```python
input = np.array([u'monday', u'tuesday', u'wednesday', u'thursday']).astype(np.object)
output = np.array([u'tuesday', u'wednesday', u'thursday']).astype(np.object)
stopwords = [u'monday']
node = onnx.helper.make_node(
'StringNormalizer',
inputs=['x'],
outputs=['y'],
is_case_sensitive=1,
stopwords=stopwords
)
expect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_casesensintive_nochangecase')
```
monday_casesensintive_upper
```python
input = np.array([u'monday', u'tuesday', u'wednesday', u'thursday']).astype(np.object)
output = np.array([u'TUESDAY', u'WEDNESDAY', u'THURSDAY']).astype(np.object)
stopwords = [u'monday']
node = onnx.helper.make_node(
'StringNormalizer',
inputs=['x'],
outputs=['y'],
case_change_action='UPPER',
is_case_sensitive=1,
stopwords=stopwords
)
expect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_casesensintive_upper')
```
monday_empty_output
```python
input = np.array([u'monday', u'monday']).astype(np.object)
output = np.array([u'']).astype(np.object)
stopwords = [u'monday']
node = onnx.helper.make_node(
'StringNormalizer',
inputs=['x'],
outputs=['y'],
case_change_action='UPPER',
is_case_sensitive=1,
stopwords=stopwords
)
expect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_empty_output')
```
monday_insensintive_upper_twodim
```python
input = np.array([u'Monday', u'tuesday', u'wednesday', u'Monday', u'tuesday', u'wednesday']).astype(np.object).reshape([1, 6])
# It does upper case cecedille, accented E
# and german umlaut but fails
# with german eszett
output = np.array([u'TUESDAY', u'WEDNESDAY', u'TUESDAY', u'WEDNESDAY']).astype(np.object).reshape([1, 4])
stopwords = [u'monday']
node = onnx.helper.make_node(
'StringNormalizer',
inputs=['x'],
outputs=['y'],
case_change_action='UPPER',
stopwords=stopwords
)
expect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_insensintive_upper_twodim')
```
nostopwords_nochangecase
```python
input = np.array([u'monday', u'tuesday']).astype(np.object)
output = input
# No stopwords. This is a NOOP
node = onnx.helper.make_node(
'StringNormalizer',
inputs=['x'],
outputs=['y'],
is_case_sensitive=1,
)
expect(node, inputs=[input], outputs=[output], name='test_strnormalizer_nostopwords_nochangecase')
```
### Sub
There are 2 test cases, listed as following:
sub
```python
node = onnx.helper.make_node(
'Sub',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.array([1, 2, 3]).astype(np.float32)
y = np.array([3, 2, 1]).astype(np.float32)
z = x - y # expected output [-2., 0., 2.]
expect(node, inputs=[x, y], outputs=[z],
name='test_sub_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(3, 4, 5).astype(np.float32)
z = x - y
expect(node, inputs=[x, y], outputs=[z],
name='test_sub')
```
sub_broadcast
```python
node = onnx.helper.make_node(
'Sub',
inputs=['x', 'y'],
outputs=['z'],
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.random.randn(5).astype(np.float32)
z = x - y
expect(node, inputs=[x, y], outputs=[z],
name='test_sub_bcast')
```
### Sum
There are 1 test cases, listed as following:
sum
```python
data_0 = np.array([3, 0, 2]).astype(np.float32)
data_1 = np.array([1, 3, 4]).astype(np.float32)
data_2 = np.array([2, 6, 6]).astype(np.float32)
result = np.array([6, 9, 12]).astype(np.float32)
node = onnx.helper.make_node(
'Sum',
inputs=['data_0', 'data_1', 'data_2'],
outputs=['result'],
)
expect(node, inputs=[data_0, data_1, data_2], outputs=[result],
name='test_sum_example')
node = onnx.helper.make_node(
'Sum',
inputs=['data_0'],
outputs=['result'],
)
expect(node, inputs=[data_0], outputs=[data_0],
name='test_sum_one_input')
result = np.add(data_0, data_1)
node = onnx.helper.make_node(
'Sum',
inputs=['data_0', 'data_1'],
outputs=['result'],
)
expect(node, inputs=[data_0, data_1], outputs=[result],
name='test_sum_two_inputs')
```
### Tan
There are 1 test cases, listed as following:
tan
```python
node = onnx.helper.make_node(
'Tan',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.tan(x)
expect(node, inputs=[x], outputs=[y],
name='test_tan_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.tan(x)
expect(node, inputs=[x], outputs=[y],
name='test_tan')
```
### Tanh
There are 1 test cases, listed as following:
tanh
```python
node = onnx.helper.make_node(
'Tanh',
inputs=['x'],
outputs=['y'],
)
x = np.array([-1, 0, 1]).astype(np.float32)
y = np.tanh(x) # expected output [-0.76159418, 0., 0.76159418]
expect(node, inputs=[x], outputs=[y],
name='test_tanh_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.tanh(x)
expect(node, inputs=[x], outputs=[y],
name='test_tanh')
```
### TfIdfVectorizer
There are 7 test cases, listed as following:
tf_batch_onlybigrams_skip0
```python
input = np.array([[1, 1, 3, 3, 3, 7], [8, 6, 7, 5, 6, 8]]).astype(np.int32)
output = np.array([[0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 1.]]).astype(np.float32)
ngram_counts = np.array([0, 4]).astype(np.int64)
ngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)
pool_int64s = np.array([2, 3, 5, 4, # unigrams
5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams
helper = TfIdfVectorizerHelper(
mode='TF',
min_gram_length=2,
max_gram_length=2,
max_skip_count=0,
ngram_counts=ngram_counts,
ngram_indexes=ngram_indexes,
pool_int64s=pool_int64s
)
node = helper.make_node_noweights()
expect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_batch_onlybigrams_skip0')
```
tf_batch_onlybigrams_skip5
```python
input = np.array([[1, 1, 3, 3, 3, 7], [8, 6, 7, 5, 6, 8]]).astype(np.int32)
output = np.array([[0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 1., 1.]]).astype(np.float32)
ngram_counts = np.array([0, 4]).astype(np.int64)
ngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)
pool_int64s = np.array([2, 3, 5, 4, # unigrams
5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams
helper = TfIdfVectorizerHelper(
mode='TF',
min_gram_length=2,
max_gram_length=2,
max_skip_count=5,
ngram_counts=ngram_counts,
ngram_indexes=ngram_indexes,
pool_int64s=pool_int64s
)
node = helper.make_node_noweights()
expect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_batch_onlybigrams_skip5')
```
tf_batch_uniandbigrams_skip5
```python
input = np.array([[1, 1, 3, 3, 3, 7], [8, 6, 7, 5, 6, 8]]).astype(np.int32)
output = np.array([[0., 3., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 1., 1., 1.]]).astype(np.float32)
ngram_counts = np.array([0, 4]).astype(np.int64)
ngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)
pool_int64s = np.array([2, 3, 5, 4, # unigrams
5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams
helper = TfIdfVectorizerHelper(
mode='TF',
min_gram_length=1,
max_gram_length=2,
max_skip_count=5,
ngram_counts=ngram_counts,
ngram_indexes=ngram_indexes,
pool_int64s=pool_int64s
)
node = helper.make_node_noweights()
expect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_batch_uniandbigrams_skip5')
```
tf_only_bigrams_skip0
```python
input = np.array([1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6, 8]).astype(np.int32)
output = np.array([0., 0., 0., 0., 1., 1., 1.]).astype(np.float32)
ngram_counts = np.array([0, 4]).astype(np.int64)
ngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)
pool_int64s = np.array([2, 3, 5, 4, # unigrams
5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams
helper = TfIdfVectorizerHelper(
mode='TF',
min_gram_length=2,
max_gram_length=2,
max_skip_count=0,
ngram_counts=ngram_counts,
ngram_indexes=ngram_indexes,
pool_int64s=pool_int64s
)
node = helper.make_node_noweights()
expect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_only_bigrams_skip0')
```
tf_onlybigrams_levelempty
```python
input = np.array([1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6, 8]).astype(np.int32)
output = np.array([1., 1., 1.]).astype(np.float32)
ngram_counts = np.array([0, 0]).astype(np.int64)
ngram_indexes = np.array([0, 1, 2]).astype(np.int64)
pool_int64s = np.array([ # unigrams none
5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams
helper = TfIdfVectorizerHelper(
mode='TF',
min_gram_length=2,
max_gram_length=2,
max_skip_count=0,
ngram_counts=ngram_counts,
ngram_indexes=ngram_indexes,
pool_int64s=pool_int64s
)
node = helper.make_node_noweights()
expect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_onlybigrams_levelempty')
```
tf_onlybigrams_skip5
```python
input = np.array([1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6, 8]).astype(np.int32)
output = np.array([0., 0., 0., 0., 1., 3., 1.]).astype(np.float32)
ngram_counts = np.array([0, 4]).astype(np.int64)
ngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)
pool_int64s = np.array([2, 3, 5, 4, # unigrams
5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams
helper = TfIdfVectorizerHelper(
mode='TF',
min_gram_length=2,
max_gram_length=2,
max_skip_count=5,
ngram_counts=ngram_counts,
ngram_indexes=ngram_indexes,
pool_int64s=pool_int64s
)
node = helper.make_node_noweights()
expect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_onlybigrams_skip5')
```
tf_uniandbigrams_skip5
```python
input = np.array([1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6, 8]).astype(np.int32)
output = np.array([0., 3., 1., 0., 1., 3., 1.]).astype(np.float32)
ngram_counts = np.array([0, 4]).astype(np.int64)
ngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)
pool_int64s = np.array([2, 3, 5, 4, # unigrams
5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams
helper = TfIdfVectorizerHelper(
mode='TF',
min_gram_length=1,
max_gram_length=2,
max_skip_count=5,
ngram_counts=ngram_counts,
ngram_indexes=ngram_indexes,
pool_int64s=pool_int64s
)
node = helper.make_node_noweights()
expect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_uniandbigrams_skip5')
```
### ThresholdedRelu
There are 2 test cases, listed as following:
default
```python
default_alpha = 1.0
node = onnx.helper.make_node(
'ThresholdedRelu',
inputs=['x'],
outputs=['y']
)
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, default_alpha, np.inf)
y[y == default_alpha] = 0
expect(node, inputs=[x], outputs=[y],
name='test_thresholdedrelu_default')
```
thresholdedrelu
```python
alpha = 2.0
node = onnx.helper.make_node(
'ThresholdedRelu',
inputs=['x'],
outputs=['y'],
alpha=alpha
)
x = np.array([-1.5, 0., 1.2, 2.0, 2.2]).astype(np.float32)
y = np.clip(x, alpha, np.inf) # expected output [0., 0., 0., 0., 2.2]
y[y == alpha] = 0
expect(node, inputs=[x], outputs=[y],
name='test_thresholdedrelu_example')
x = np.random.randn(3, 4, 5).astype(np.float32)
y = np.clip(x, alpha, np.inf)
y[y == alpha] = 0
expect(node, inputs=[x], outputs=[y],
name='test_thresholdedrelu')
```
### Tile
There are 2 test cases, listed as following:
tile
```python
node = onnx.helper.make_node(
'Tile',
inputs=['x', 'y'],
outputs=['z']
)
x = np.random.rand(2, 3, 4, 5).astype(np.float32)
repeats = np.random.randint(low=1, high=10, size=(np.ndim(x),)).astype(np.int64)
z = np.tile(x, repeats)
expect(node,
inputs=[x, repeats],
outputs=[z],
name='test_tile')
```
tile_precomputed
```python
node = onnx.helper.make_node(
'Tile',
inputs=['x', 'y'],
outputs=['z']
)
x = np.array([
[0, 1],
[2, 3]
], dtype=np.float32)
repeats = np.array([2, 2], dtype=np.int64)
z = np.array([
[0, 1, 0, 1],
[2, 3, 2, 3],
[0, 1, 0, 1],
[2, 3, 2, 3]
], dtype=np.float32)
expect(node,
inputs=[x, repeats],
outputs=[z],
name='test_tile_precomputed')
```
### TopK
There are 3 test cases, listed as following:
top_k
```python
axis = 1
largest = 1
k = 3
node = onnx.helper.make_node(
'TopK',
inputs=['x', 'k'],
outputs=['values', 'indices'],
axis=axis
)
X = np.array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
], dtype=np.float32)
K = np.array([k], dtype=np.int64)
values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
#print(values_ref)
#[[ 3. 2. 1.]
# [ 7. 6. 5.]
# [11. 10. 9.]]
#print(indices_ref)
#[[3 2 1]
# [3 2 1]
# [3 2 1]]
expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
name='test_top_k')
```
top_k_negative_axis
```python
axis = -1
largest = 1
k = 3
node = onnx.helper.make_node(
'TopK',
inputs=['x', 'k'],
outputs=['values', 'indices'],
axis=axis
)
X = np.array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
], dtype=np.float32)
K = np.array([k], dtype=np.int64)
values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
# print(values_ref)
#[[ 3. 2. 1.]
# [ 7. 6. 5.]
# [11. 10. 9.]]
# print(indices_ref)
#[[3 2 1]
# [3 2 1]
# [3 2 1]]
expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
name='test_top_k_negative_axis')
```
top_k_smallest
```python
axis = 1
largest = 0
sorted = 1
k = 3
node = onnx.helper.make_node(
'TopK',
inputs=['x', 'k'],
outputs=['values', 'indices'],
axis=axis,
largest=largest,
sorted=sorted
)
X = np.array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[11, 10, 9, 8],
], dtype=np.float32)
K = np.array([k], dtype=np.int64)
values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
#print(values_ref)
#[[ 0. 1. 2.]
# [ 4. 5. 6.]
# [ 8. 9. 10.]]
#print(indices_ref)
#[[0 1 2]
# [0 1 2]
# [3 2 1]]
expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
name='test_top_k_smallest')
```
### Transpose
There are 2 test cases, listed as following:
all_permutations
```python
shape = (2, 3, 4)
data = np.random.random_sample(shape).astype(np.float32)
permutations = list(itertools.permutations(np.arange(len(shape))))
for i in range(len(permutations)):
node = onnx.helper.make_node(
'Transpose',
inputs=['data'],
outputs=['transposed'],
perm=permutations[i]
)
transposed = np.transpose(data, permutations[i])
expect(node, inputs=[data], outputs=[transposed],
name='test_transpose_all_permutations_' + str(i))
```
default
```python
shape = (2, 3, 4)
data = np.random.random_sample(shape).astype(np.float32)
node = onnx.helper.make_node(
'Transpose',
inputs=['data'],
outputs=['transposed']
)
transposed = np.transpose(data)
expect(node, inputs=[data], outputs=[transposed],
name='test_transpose_default')
```
### Unique
There are 5 test cases, listed as following:
not_sorted_without_axis
```python
node_not_sorted = onnx.helper.make_node(
'Unique',
inputs=['X'],
outputs=['Y', 'indices', 'inverse_indices', 'counts'],
sorted=0
)
# numpy unique does not retain original order (it sorts the output unique values)
# https://github.com/numpy/numpy/issues/8621
# we need to recover unsorted output and indices
x = np.array([2.0, 1.0, 1.0, 3.0, 4.0, 3.0], dtype=np.float32)
y, indices, inverse_indices, counts = np.unique(x, True, True, True)
# prepare index mapping from sorted to unsorted
argsorted_indices = np.argsort(indices)
inverse_indices_map = {i: si for i, si in zip(argsorted_indices, np.arange(len(argsorted_indices)))}
y = np.take(x, indices, axis=0)
indices = indices[argsorted_indices]
inverse_indices = np.asarray([inverse_indices_map[i] for i in inverse_indices], dtype=np.int64)
counts = counts[argsorted_indices]
# print(y)
# [2.0, 1.0, 3.0, 4.0]
# print(indices)
# [0 1 3 4]
# print(inverse_indices)
# [0, 1, 1, 2, 3, 2]
# print(counts)
# [1, 2, 2, 1]
expect(node_not_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_not_sorted_without_axis')
```
sorted_with_axis
```python
node_sorted = onnx.helper.make_node(
'Unique',
inputs=['X'],
outputs=['Y', 'indices', 'inverse_indices', 'counts'],
sorted=1,
axis=0
)
x = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]], dtype=np.float32)
y, indices, inverse_indices, counts = np.unique(x, True, True, True, axis=0)
# print(y)
# [[1. 0. 0.]
# [2. 3. 4.]]
# print(indices)
# [0 2]
# print(inverse_indices)
# [0 0 1]
# print(counts)
# [2 1]
expect(node_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_sorted_with_axis')
```
sorted_with_axis_3d
```python
node_sorted = onnx.helper.make_node(
'Unique',
inputs=['X'],
outputs=['Y', 'indices', 'inverse_indices', 'counts'],
sorted=1,
axis=1
)
x = np.array([[[1., 1.], [0., 1.], [2., 1.], [0., 1.]],
[[1., 1.], [0., 1.], [2., 1.], [0., 1.]]], dtype=np.float32)
y, indices, inverse_indices, counts = np.unique(x, True, True, True, axis=1)
# print(y)
# [[[0. 1.]
# [1. 1.]
# [2. 1.]]
# [[0. 1.]
# [1. 1.]
# [2. 1.]]]
# print(indices)
# [1 0 2]
# print(inverse_indices)
# [1 0 2 0]
# print(counts)
# [2 1 1]
expect(node_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_sorted_with_axis_3d')
```
sorted_with_negative_axis
```python
node_sorted = onnx.helper.make_node(
'Unique',
inputs=['X'],
outputs=['Y', 'indices', 'inverse_indices', 'counts'],
sorted=1,
axis=-1
)
x = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 3]], dtype=np.float32)
y, indices, inverse_indices, counts = np.unique(x, True, True, True, axis=-1)
# print(y)
# [[0. 1.]
# [0. 1.]
# [3. 2.]]
# print(indices)
# [1 0]
# print(inverse_indices)
# [1 0 0]
# print(counts)
# [2 1]
expect(node_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_sorted_with_negative_axis')
```
sorted_without_axis
```python
node_sorted = onnx.helper.make_node(
'Unique',
inputs=['X'],
outputs=['Y', 'indices', 'inverse_indices', 'counts']
)
x = np.array([2.0, 1.0, 1.0, 3.0, 4.0, 3.0], dtype=np.float32)
y, indices, inverse_indices, counts = np.unique(x, True, True, True)
expect(node_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_sorted_without_axis')
```
### Unsqueeze
There are 5 test cases, listed as following:
unsqueeze_negative_axes
```python
node = onnx.helper.make_node(
'Unsqueeze',
inputs=['x'],
outputs=['y'],
axes=[-2],
)
x = np.random.randn(1, 3, 1, 5).astype(np.float32)
y = np.expand_dims(x, axis=-2)
expect(node, inputs=[x], outputs=[y],
name='test_unsqueeze_negative_axes')
```
unsqueeze_one_axis
```python
x = np.random.randn(3, 4, 5).astype(np.float32)
for i in range(x.ndim):
node = onnx.helper.make_node(
'Unsqueeze',
inputs=['x'],
outputs=['y'],
axes=[i],
)
y = np.expand_dims(x, axis=i)
expect(node, inputs=[x], outputs=[y],
name='test_unsqueeze_axis_' + str(i))
```
unsqueeze_three_axes
```python
x = np.random.randn(3, 4, 5).astype(np.float32)
node = onnx.helper.make_node(
'Unsqueeze',
inputs=['x'],
outputs=['y'],
axes=[2, 4, 5],
)
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], outputs=[y],
name='test_unsqueeze_three_axes')
```
unsqueeze_two_axes
```python
x = np.random.randn(3, 4, 5).astype(np.float32)
node = onnx.helper.make_node(
'Unsqueeze',
inputs=['x'],
outputs=['y'],
axes=[1, 4],
)
y = np.expand_dims(x, axis=1)
y = np.expand_dims(y, axis=4)
expect(node, inputs=[x], outputs=[y],
name='test_unsqueeze_two_axes')
```
unsqueeze_unsorted_axes
```python
x = np.random.randn(3, 4, 5).astype(np.float32)
node = onnx.helper.make_node(
'Unsqueeze',
inputs=['x'],
outputs=['y'],
axes=[5, 4, 2],
)
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], outputs=[y],
name='test_unsqueeze_unsorted_axes')
```
### Upsample
There are 1 test cases, listed as following:
nearest
```python
node = onnx.helper.make_node(
'Upsample',
inputs=['X', 'scales'],
outputs=['Y'],
mode='nearest',
)
data = np.array([[[
[1, 2],
[3, 4],
]]], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)
output = np.array([[[
[1, 1, 1, 2, 2, 2],
[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4],
[3, 3, 3, 4, 4, 4],
]]], dtype=np.float32)
expect(node, inputs=[data, scales], outputs=[output],
name='test_upsample_nearest', opset_imports=[helper.make_opsetid("", 9)])
```
### Where
There are 2 test cases, listed as following:
long
```python
node = onnx.helper.make_node(
'Where',
inputs=['condition', 'x', 'y'],
outputs=['z'],
)
condition = np.array([[1, 0], [1, 1]], dtype=np.bool)
x = np.array([[1, 2], [3, 4]], dtype=np.int64)
y = np.array([[9, 8], [7, 6]], dtype=np.int64)
z = np.where(condition, x, y) # expected output [[1, 8], [3, 4]]
expect(node, inputs=[condition, x, y], outputs=[z],
name='test_where_long_example')
```
where
```python
node = onnx.helper.make_node(
'Where',
inputs=['condition', 'x', 'y'],
outputs=['z'],
)
condition = np.array([[1, 0], [1, 1]], dtype=np.bool)
x = np.array([[1, 2], [3, 4]], dtype=np.float32)
y = np.array([[9, 8], [7, 6]], dtype=np.float32)
z = np.where(condition, x, y) # expected output [[1, 8], [3, 4]]
expect(node, inputs=[condition, x, y], outputs=[z],
name='test_where_example')
```
### Xor
There are 2 test cases, listed as following:
xor
```python
node = onnx.helper.make_node(
'Xor',
inputs=['x', 'y'],
outputs=['xor'],
)
# 2d
x = (np.random.randn(3, 4) > 0).astype(np.bool)
y = (np.random.randn(3, 4) > 0).astype(np.bool)
z = np.logical_xor(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_xor2d')
# 3d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
z = np.logical_xor(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_xor3d')
# 4d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
z = np.logical_xor(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_xor4d')
```
xor_broadcast
```python
node = onnx.helper.make_node(
'Xor',
inputs=['x', 'y'],
outputs=['xor'],
)
# 3d vs 1d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(5) > 0).astype(np.bool)
z = np.logical_xor(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_xor_bcast3v1d')
# 3d vs 2d
x = (np.random.randn(3, 4, 5) > 0).astype(np.bool)
y = (np.random.randn(4, 5) > 0).astype(np.bool)
z = np.logical_xor(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_xor_bcast3v2d')
# 4d vs 2d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(5, 6) > 0).astype(np.bool)
z = np.logical_xor(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_xor_bcast4v2d')
# 4d vs 3d
x = (np.random.randn(3, 4, 5, 6) > 0).astype(np.bool)
y = (np.random.randn(4, 5, 6) > 0).astype(np.bool)
z = np.logical_xor(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_xor_bcast4v3d')
# 4d vs 4d
x = (np.random.randn(1, 4, 1, 6) > 0).astype(np.bool)
y = (np.random.randn(3, 1, 5, 6) > 0).astype(np.bool)
z = np.logical_xor(x, y)
expect(node, inputs=[x, y], outputs=[z],
name='test_xor_bcast4v4d')
```
## 💔No Cover Common Operators
### ConcatFromSequence (call for test cases)
### GlobalLpPool (call for test cases)
### If (call for test cases)
### Loop (call for test cases)
### LpNormalization (call for test cases)
### LpPool (call for test cases)
### MaxRoiPool (call for test cases)
### Multinomial (random generator operator)
### RandomNormal (random generator operator)
### RandomNormalLike (random generator operator)
### RandomUniform (random generator operator)
### RandomUniformLike (random generator operator)
### SequenceAt (call for test cases)
### SequenceConstruct (call for test cases)
### SequenceEmpty (call for test cases)
### SequenceErase (call for test cases)
### SequenceInsert (call for test cases)
### SequenceLength (call for test cases)
### SpaceToDepth (call for test cases)
### SplitToSequence (call for test cases)
## 💚Covered Experimental Operators
## 💔No Cover Experimental Operators
# Model Test Coverage
## bvlc_alexnet
bvlc_alexnet has 24 nodes. Of these, 24 are covered by node tests (100.0%)
nodes
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 1
kernel_shape: 3
pads: 3
strides: 2
Dropout: 1 out of 1 attributes covered
ratio: 1
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 1
beta: 1
bias: 1
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 1
pads: 2
storage_order: 0
strides: 1
## densenet121
densenet121 has 910 nodes. Of these, 910 are covered by node tests (100.0%)
nodes
AveragePool: 3 out of 6 attributes covered
auto_pad: 0
ceil_mode: 0
count_include_pad: 0
kernel_shape: 1
pads: 1
strides: 1
BatchNormalization: 1 out of 2 attributes covered
epsilon: 1
momentum: 0
Concat: 1 out of 1 attributes covered
axis: 1
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 1
kernel_shape: 5
pads: 4
strides: 3
Dropout: 1 out of 1 attributes covered
ratio: 1
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 1
beta: 1
bias: 1
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 1
pads: 3
storage_order: 0
strides: 1
Unsqueeze: 1 out of 1 attributes covered
axes: 1
## inception_v1
inception_v1 has 144 nodes. Of these, 144 are covered by node tests (100.0%)
nodes
AveragePool: 3 out of 6 attributes covered
auto_pad: 0
ceil_mode: 0
count_include_pad: 0
kernel_shape: 2
pads: 2
strides: 2
BatchNormalization: 1 out of 2 attributes covered
epsilon: 1
momentum: 0
Concat: 1 out of 1 attributes covered
axis: 1
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 1
kernel_shape: 5
pads: 4
strides: 3
Dropout: 1 out of 1 attributes covered
ratio: 2
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 1
beta: 1
bias: 1
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 1
pads: 3
storage_order: 0
strides: 2
Unsqueeze: 1 out of 1 attributes covered
axes: 1
## inception_v2
inception_v2 has 509 nodes. Of these, 509 are covered by node tests (100.0%)
nodes
AveragePool: 3 out of 6 attributes covered
auto_pad: 0
ceil_mode: 0
count_include_pad: 0
kernel_shape: 3
pads: 3
strides: 2
BatchNormalization: 1 out of 2 attributes covered
epsilon: 1
momentum: 0
Concat: 1 out of 1 attributes covered
axis: 1
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 1
kernel_shape: 5
pads: 4
strides: 3
Dropout: 1 out of 1 attributes covered
ratio: 2
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 1
beta: 1
bias: 1
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 1
pads: 3
storage_order: 0
strides: 2
Unsqueeze: 1 out of 1 attributes covered
axes: 1
## resnet50
resnet50 has 176 nodes. Of these, 176 are covered by node tests (100.0%)
nodes
AveragePool: 3 out of 6 attributes covered
auto_pad: 0
ceil_mode: 0
count_include_pad: 0
kernel_shape: 3
pads: 3
strides: 2
BatchNormalization: 1 out of 2 attributes covered
epsilon: 2
momentum: 0
Concat: 1 out of 1 attributes covered
axis: 1
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 1
kernel_shape: 5
pads: 4
strides: 3
Dropout: 1 out of 1 attributes covered
ratio: 2
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 1
beta: 1
bias: 1
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 1
pads: 3
storage_order: 0
strides: 2
Unsqueeze: 1 out of 1 attributes covered
axes: 1
## shufflenet
shufflenet has 203 nodes. Of these, 203 are covered by node tests (100.0%)
nodes
AveragePool: 3 out of 6 attributes covered
auto_pad: 0
ceil_mode: 0
count_include_pad: 0
kernel_shape: 3
pads: 3
strides: 2
BatchNormalization: 1 out of 2 attributes covered
epsilon: 2
momentum: 0
Concat: 1 out of 1 attributes covered
axis: 1
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 6
kernel_shape: 5
pads: 4
strides: 3
Dropout: 1 out of 1 attributes covered
ratio: 2
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 1
beta: 1
bias: 1
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 1
pads: 3
storage_order: 0
strides: 2
Transpose: 1 out of 1 attributes covered
perm: 1
Unsqueeze: 1 out of 1 attributes covered
axes: 1
## squeezenet_old
squeezenet_old has 66 nodes. Of these, 66 are covered by node tests (100.0%)
nodes
AveragePool: 3 out of 6 attributes covered
auto_pad: 0
ceil_mode: 0
count_include_pad: 0
kernel_shape: 3
pads: 3
strides: 2
BatchNormalization: 1 out of 2 attributes covered
epsilon: 2
momentum: 0
Concat: 1 out of 1 attributes covered
axis: 1
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 6
kernel_shape: 5
pads: 4
strides: 3
Dropout: 1 out of 1 attributes covered
ratio: 2
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 1
beta: 1
bias: 1
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 1
pads: 3
storage_order: 0
strides: 2
Transpose: 1 out of 1 attributes covered
perm: 1
Unsqueeze: 1 out of 1 attributes covered
axes: 1
## vgg19
vgg19 has 46 nodes. Of these, 46 are covered by node tests (100.0%)
nodes
AveragePool: 3 out of 6 attributes covered
auto_pad: 0
ceil_mode: 0
count_include_pad: 0
kernel_shape: 3
pads: 3
strides: 2
BatchNormalization: 1 out of 2 attributes covered
epsilon: 2
momentum: 0
Concat: 1 out of 1 attributes covered
axis: 1
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 6
kernel_shape: 5
pads: 4
strides: 3
Dropout: 1 out of 1 attributes covered
ratio: 2
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 1
beta: 1
bias: 1
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 2
pads: 3
storage_order: 0
strides: 2
Transpose: 1 out of 1 attributes covered
perm: 1
Unsqueeze: 1 out of 1 attributes covered
axes: 1
## zfnet512
zfnet512 has 22 nodes. Of these, 22 are covered by node tests (100.0%)
nodes
AveragePool: 3 out of 6 attributes covered
auto_pad: 0
ceil_mode: 0
count_include_pad: 0
kernel_shape: 3
pads: 3
strides: 2
BatchNormalization: 1 out of 2 attributes covered
epsilon: 2
momentum: 0
Concat: 1 out of 1 attributes covered
axis: 1
Conv: 4 out of 6 attributes covered
auto_pad: 0
dilations: 0
group: 6
kernel_shape: 5
pads: 4
strides: 3
Dropout: 1 out of 1 attributes covered
ratio: 2
Gemm: 1 out of 4 attributes covered
alpha: 0
beta: 0
transA: 0
transB: 1
LRN: 4 out of 4 attributes covered
alpha: 2
beta: 1
bias: 2
size: 1
MaxPool: 3 out of 7 attributes covered
auto_pad: 0
ceil_mode: 0
dilations: 0
kernel_shape: 2
pads: 3
storage_order: 0
strides: 2
Transpose: 1 out of 1 attributes covered
perm: 1
Unsqueeze: 1 out of 1 attributes covered
axes: 1
# Overall Test Coverage
## To be filled.