Flatten#
Domain:
ai.onnxSince version: 25
Flattens the input tensor into a 2D matrix. If input tensor has shape (d_0, d_1, … d_n) then the output will have shape (d_0 X d_1 … d(axis-1), d_axis X d(axis+1) … X dn).
Inputs
input (T): A tensor of rank >= axis.
Outputs
output (T): A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output.
Attributes
axis (int): Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [-r, r], where r is the rank of the input tensor. Negative value means counting dimensions from the back. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 … d_n), where the shape of the input tensor is (d_0, d_1, … d_n).
Type Constraints
T: Constrain input and output to all tensor types up to IRv13. Allowed types: tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(float8e8m0), tensor(int16), tensor(int2), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint2), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8).
Examples#
test_cc_flatten_axis0
Node:
Flatten(a) -> (b)
Attributes:
axis = 0
Inputs:
a: shape=(2, 3, 4, 5), dtype=float32
[[[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
[-22.5, -22. , -21.5, -21. , -20.5]],
[[-20. , -19.5, -19. , -18.5, -18. ],
[-17.5, -17. , -16.5, -16. , -15.5],
[-15. , -14.5, -14. , -13.5, -13. ],
[-12.5, -12. , -11.5, -11. , -10.5]],
[[-10. , -9.5, -9. , -8.5, -8. ],
[ -7.5, -7. , -6.5, -6. , -5.5],
[ -5. , -4.5, -4. , -3.5, -3. ],
[ -2.5, -2. , -1.5, -1. , -0.5]]],
[[[ 0. , 0.5, 1. , 1.5, 2. ],
[ 2.5, 3. , 3.5, 4. , 4.5],
[ 5. , 5.5, 6. , 6.5, 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. , 19.5]],
[[ 20. , 20.5, 21. , 21.5, 22. ],
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]]]
Outputs:
b: shape=(1, 120), dtype=float32
[[-30. , -29.5, -29. , ..., 28.5, 29. , 29.5]]
test_cc_flatten_axis1
Node:
Flatten(a) -> (b)
Attributes:
axis = 1
Inputs:
a: shape=(2, 3, 4, 5), dtype=float32
[[[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
[-22.5, -22. , -21.5, -21. , -20.5]],
[[-20. , -19.5, -19. , -18.5, -18. ],
[-17.5, -17. , -16.5, -16. , -15.5],
[-15. , -14.5, -14. , -13.5, -13. ],
[-12.5, -12. , -11.5, -11. , -10.5]],
[[-10. , -9.5, -9. , -8.5, -8. ],
[ -7.5, -7. , -6.5, -6. , -5.5],
[ -5. , -4.5, -4. , -3.5, -3. ],
[ -2.5, -2. , -1.5, -1. , -0.5]]],
[[[ 0. , 0.5, 1. , 1.5, 2. ],
[ 2.5, 3. , 3.5, 4. , 4.5],
[ 5. , 5.5, 6. , 6.5, 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. , 19.5]],
[[ 20. , 20.5, 21. , 21.5, 22. ],
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]]]
Outputs:
b: shape=(2, 60), dtype=float32
[[-30. , -29.5, -29. , ..., -1.5, -1. , -0.5],
[ 0. , 0.5, 1. , ..., 28.5, 29. , 29.5]]
test_cc_flatten_axis2
Node:
Flatten(a) -> (b)
Attributes:
axis = 2
Inputs:
a: shape=(2, 3, 4, 5), dtype=float32
[[[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
[-22.5, -22. , -21.5, -21. , -20.5]],
[[-20. , -19.5, -19. , -18.5, -18. ],
[-17.5, -17. , -16.5, -16. , -15.5],
[-15. , -14.5, -14. , -13.5, -13. ],
[-12.5, -12. , -11.5, -11. , -10.5]],
[[-10. , -9.5, -9. , -8.5, -8. ],
[ -7.5, -7. , -6.5, -6. , -5.5],
[ -5. , -4.5, -4. , -3.5, -3. ],
[ -2.5, -2. , -1.5, -1. , -0.5]]],
[[[ 0. , 0.5, 1. , 1.5, 2. ],
[ 2.5, 3. , 3.5, 4. , 4.5],
[ 5. , 5.5, 6. , 6.5, 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. , 19.5]],
[[ 20. , 20.5, 21. , 21.5, 22. ],
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]]]
Outputs:
b: shape=(6, 20), dtype=float32
[[-30. , -29.5, -29. , ..., -21.5, -21. , -20.5],
[-20. , -19.5, -19. , ..., -11.5, -11. , -10.5],
[-10. , -9.5, -9. , ..., -1.5, -1. , -0.5],
[ 0. , 0.5, 1. , ..., 8.5, 9. , 9.5],
[ 10. , 10.5, 11. , ..., 18.5, 19. , 19.5],
[ 20. , 20.5, 21. , ..., 28.5, 29. , 29.5]]
test_cc_flatten_axis3
Node:
Flatten(a) -> (b)
Attributes:
axis = 3
Inputs:
a: shape=(2, 3, 4, 5), dtype=float32
[[[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
[-22.5, -22. , -21.5, -21. , -20.5]],
[[-20. , -19.5, -19. , -18.5, -18. ],
[-17.5, -17. , -16.5, -16. , -15.5],
[-15. , -14.5, -14. , -13.5, -13. ],
[-12.5, -12. , -11.5, -11. , -10.5]],
[[-10. , -9.5, -9. , -8.5, -8. ],
[ -7.5, -7. , -6.5, -6. , -5.5],
[ -5. , -4.5, -4. , -3.5, -3. ],
[ -2.5, -2. , -1.5, -1. , -0.5]]],
[[[ 0. , 0.5, 1. , 1.5, 2. ],
[ 2.5, 3. , 3.5, 4. , 4.5],
[ 5. , 5.5, 6. , 6.5, 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. , 19.5]],
[[ 20. , 20.5, 21. , 21.5, 22. ],
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]]]
Outputs:
b: shape=(24, 5), dtype=float32
[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
...,
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]
test_cc_flatten_default_axis
Node:
Flatten(a) -> (b)
Inputs:
a: shape=(5, 4, 3, 2), dtype=float32
[[[[-30. , -29.5],
[-29. , -28.5],
[-28. , -27.5]],
[[-27. , -26.5],
[-26. , -25.5],
[-25. , -24.5]],
[[-24. , -23.5],
[-23. , -22.5],
[-22. , -21.5]],
[[-21. , -20.5],
[-20. , -19.5],
[-19. , -18.5]]],
[[[-18. , -17.5],
[-17. , -16.5],
[-16. , -15.5]],
[[-15. , -14.5],
[-14. , -13.5],
[-13. , -12.5]],
[[-12. , -11.5],
[-11. , -10.5],
[-10. , -9.5]],
[[ -9. , -8.5],
[ -8. , -7.5],
[ -7. , -6.5]]],
[[[ -6. , -5.5],
[ -5. , -4.5],
[ -4. , -3.5]],
[[ -3. , -2.5],
[ -2. , -1.5],
[ -1. , -0.5]],
[[ 0. , 0.5],
[ 1. , 1.5],
[ 2. , 2.5]],
[[ 3. , 3.5],
[ 4. , 4.5],
[ 5. , 5.5]]],
[[[ 6. , 6.5],
[ 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. , 19.5],
[ 20. , 20.5]],
[[ 21. , 21.5],
[ 22. , 22.5],
[ 23. , 23.5]],
[[ 24. , 24.5],
[ 25. , 25.5],
[ 26. , 26.5]],
[[ 27. , 27.5],
[ 28. , 28.5],
[ 29. , 29.5]]]]
Outputs:
b: shape=(5, 24), dtype=float32
[[-30. , -29.5, -29. , ..., -19.5, -19. , -18.5],
[-18. , -17.5, -17. , ..., -7.5, -7. , -6.5],
[ -6. , -5.5, -5. , ..., 4.5, 5. , 5.5],
[ 6. , 6.5, 7. , ..., 16.5, 17. , 17.5],
[ 18. , 18.5, 19. , ..., 28.5, 29. , 29.5]]
test_cc_flatten_negative_axis1
Node:
Flatten(a) -> (b)
Attributes:
axis = -1
Inputs:
a: shape=(2, 3, 4, 5), dtype=float32
[[[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
[-22.5, -22. , -21.5, -21. , -20.5]],
[[-20. , -19.5, -19. , -18.5, -18. ],
[-17.5, -17. , -16.5, -16. , -15.5],
[-15. , -14.5, -14. , -13.5, -13. ],
[-12.5, -12. , -11.5, -11. , -10.5]],
[[-10. , -9.5, -9. , -8.5, -8. ],
[ -7.5, -7. , -6.5, -6. , -5.5],
[ -5. , -4.5, -4. , -3.5, -3. ],
[ -2.5, -2. , -1.5, -1. , -0.5]]],
[[[ 0. , 0.5, 1. , 1.5, 2. ],
[ 2.5, 3. , 3.5, 4. , 4.5],
[ 5. , 5.5, 6. , 6.5, 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. , 19.5]],
[[ 20. , 20.5, 21. , 21.5, 22. ],
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]]]
Outputs:
b: shape=(24, 5), dtype=float32
[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
...,
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]
test_cc_flatten_negative_axis2
Node:
Flatten(a) -> (b)
Attributes:
axis = -2
Inputs:
a: shape=(2, 3, 4, 5), dtype=float32
[[[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
[-22.5, -22. , -21.5, -21. , -20.5]],
[[-20. , -19.5, -19. , -18.5, -18. ],
[-17.5, -17. , -16.5, -16. , -15.5],
[-15. , -14.5, -14. , -13.5, -13. ],
[-12.5, -12. , -11.5, -11. , -10.5]],
[[-10. , -9.5, -9. , -8.5, -8. ],
[ -7.5, -7. , -6.5, -6. , -5.5],
[ -5. , -4.5, -4. , -3.5, -3. ],
[ -2.5, -2. , -1.5, -1. , -0.5]]],
[[[ 0. , 0.5, 1. , 1.5, 2. ],
[ 2.5, 3. , 3.5, 4. , 4.5],
[ 5. , 5.5, 6. , 6.5, 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. , 19.5]],
[[ 20. , 20.5, 21. , 21.5, 22. ],
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]]]
Outputs:
b: shape=(6, 20), dtype=float32
[[-30. , -29.5, -29. , ..., -21.5, -21. , -20.5],
[-20. , -19.5, -19. , ..., -11.5, -11. , -10.5],
[-10. , -9.5, -9. , ..., -1.5, -1. , -0.5],
[ 0. , 0.5, 1. , ..., 8.5, 9. , 9.5],
[ 10. , 10.5, 11. , ..., 18.5, 19. , 19.5],
[ 20. , 20.5, 21. , ..., 28.5, 29. , 29.5]]
test_cc_flatten_negative_axis3
Node:
Flatten(a) -> (b)
Attributes:
axis = -3
Inputs:
a: shape=(2, 3, 4, 5), dtype=float32
[[[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
[-22.5, -22. , -21.5, -21. , -20.5]],
[[-20. , -19.5, -19. , -18.5, -18. ],
[-17.5, -17. , -16.5, -16. , -15.5],
[-15. , -14.5, -14. , -13.5, -13. ],
[-12.5, -12. , -11.5, -11. , -10.5]],
[[-10. , -9.5, -9. , -8.5, -8. ],
[ -7.5, -7. , -6.5, -6. , -5.5],
[ -5. , -4.5, -4. , -3.5, -3. ],
[ -2.5, -2. , -1.5, -1. , -0.5]]],
[[[ 0. , 0.5, 1. , 1.5, 2. ],
[ 2.5, 3. , 3.5, 4. , 4.5],
[ 5. , 5.5, 6. , 6.5, 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. , 19.5]],
[[ 20. , 20.5, 21. , 21.5, 22. ],
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]]]
Outputs:
b: shape=(2, 60), dtype=float32
[[-30. , -29.5, -29. , ..., -1.5, -1. , -0.5],
[ 0. , 0.5, 1. , ..., 28.5, 29. , 29.5]]
test_cc_flatten_negative_axis4
Node:
Flatten(a) -> (b)
Attributes:
axis = -4
Inputs:
a: shape=(2, 3, 4, 5), dtype=float32
[[[[-30. , -29.5, -29. , -28.5, -28. ],
[-27.5, -27. , -26.5, -26. , -25.5],
[-25. , -24.5, -24. , -23.5, -23. ],
[-22.5, -22. , -21.5, -21. , -20.5]],
[[-20. , -19.5, -19. , -18.5, -18. ],
[-17.5, -17. , -16.5, -16. , -15.5],
[-15. , -14.5, -14. , -13.5, -13. ],
[-12.5, -12. , -11.5, -11. , -10.5]],
[[-10. , -9.5, -9. , -8.5, -8. ],
[ -7.5, -7. , -6.5, -6. , -5.5],
[ -5. , -4.5, -4. , -3.5, -3. ],
[ -2.5, -2. , -1.5, -1. , -0.5]]],
[[[ 0. , 0.5, 1. , 1.5, 2. ],
[ 2.5, 3. , 3.5, 4. , 4.5],
[ 5. , 5.5, 6. , 6.5, 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. , 19.5]],
[[ 20. , 20.5, 21. , 21.5, 22. ],
[ 22.5, 23. , 23.5, 24. , 24.5],
[ 25. , 25.5, 26. , 26.5, 27. ],
[ 27.5, 28. , 28.5, 29. , 29.5]]]]
Outputs:
b: shape=(1, 120), dtype=float32
[[-30. , -29.5, -29. , ..., 28.5, 29. , 29.5]]
Differences with previous version (24)#
SchemaDiff: Flatten (domain 'ai.onnx')
old version: 24
new version: 25
breaking: no
Type constraints:
changed ‘T’: added types: [‘tensor(int2)’, ‘tensor(uint2)’]