NonZero#

  • Domain: ai.onnx

  • Since version: 13

Returns the indices of the elements that are non-zero (in row-major order - by dimension). NonZero behaves similar to numpy.nonzero: https://docs.scipy.org/doc/numpy/reference/generated/numpy.nonzero.html, but for scalar input, NonZero produces output shape (0, N) instead of (1, N), which is different from Numpy’s behavior.

Inputs

  • X (T): input

Outputs

  • Y (tensor(int64)): output

Type Constraints

  • T: Constrain to all tensor types. Allowed types: tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

Examples#

test_cc_nonzero_1d

Node:
  NonZero(X) -> (Y)
Inputs:
  X: shape=(5,), dtype=float32
    [ 0.,  1.,  0., -1.,  2.]

Outputs:
  Y: shape=(1, 3), dtype=int64
    [[1, 3, 4]]

test_cc_nonzero_2d

Node:
  NonZero(X) -> (Y)
Inputs:
  X: shape=(2, 2), dtype=float32
    [[1., 0.],
     [1., 1.]]

Outputs:
  Y: shape=(2, 3), dtype=int64
    [[0, 1, 1],
     [0, 0, 1]]

test_cc_nonzero_bool

Node:
  NonZero(X) -> (Y)
Inputs:
  X: shape=(2, 3), dtype=bool
    [[ True, False,  True],
     [False,  True, False]]

Outputs:
  Y: shape=(2, 3), dtype=int64
    [[0, 0, 1],
     [0, 2, 1]]

test_cc_nonzero_example

Node:
  NonZero(condition) -> (result)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True,  True]]

Outputs:
  result: shape=(2, 3), dtype=int64
    [[0, 1, 1],
     [0, 0, 1]]

test_cc_nonzero_int64

Node:
  NonZero(X) -> (Y)
Inputs:
  X: shape=(2, 3), dtype=int64
    [[0, 1, 2],
     [0, 0, 3]]

Outputs:
  Y: shape=(2, 3), dtype=int64
    [[0, 0, 1],
     [1, 2, 2]]

Differences with previous version (9)#

SchemaDiff: NonZero (domain 'ai.onnx')

  • old version: 9

  • new version: 13

  • breaking: no

Type constraints:

  • changed ‘T’: added types: [‘tensor(bfloat16)’]

Version History#