onnx.numpy_helper

from_array(array, /[, name])

Converts an array into a TensorProto including

from_dict(dict_[, name])

Converts a Python dictionary into a map def.

from_list(lst[, name, dtype])

Converts a list into a sequence def.

from_optional(opt[, name, dtype])

Converts an optional value into a Optional def.

to_array(tensor[, base_dir])

Converts a tensor def object to a numpy array.

to_dict(map_proto[, base_dir])

Converts a map def to a Python dictionary.

to_list(sequence[, base_dir])

Converts a sequence def to a Python list.

to_optional(optional[, base_dir])

Converts an optional def to a Python optional.

array

onnx.numpy_helper.from_array(array: ndarray, /, name: str | None = None) → TensorProto[source]

Converts an array into a TensorProto including

Parameters:
  • array – a numpy array.

  • name – (optional) the name of the tensor.

Returns:

the converted tensor def.

Return type:

TensorProto

onnx.numpy_helper.to_array(tensor: TensorProto, base_dir: str | None = None) → ndarray[source]

Converts a tensor def object to a numpy array.

This function uses ml_dtypes if the dtype is not a native numpy dtype.

Parameters:
  • tensor – a TensorProto object.

  • base_dir – directory containing external tensor data. This must be provided explicitly for an external tensor. Pass an empty string to use the current working directory intentionally.

Returns:

the converted array.

Return type:

arr

Arrays with data types not supported natively by NumPy will be return with ml_dtypes dtypes.

sequence

onnx.numpy_helper.to_list(sequence: SequenceProto, base_dir: str | None = None) → list[Any][source]

Converts a sequence def to a Python list.

Parameters:
  • sequence – a SequenceProto object.

  • base_dir – directory containing external tensor data.

Returns:

the converted list.

Return type:

list

onnx.numpy_helper.from_list(lst: list[Any], name: str | None = None, dtype: int | None = None) → SequenceProto[source]

Converts a list into a sequence def.

Parameters:
  • lst – a Python list

  • name – (optional) the name of the sequence.

  • dtype – (optional) type of element in the input list, used for specifying sequence values when converting an empty list.

Returns:

the converted sequence def.

Return type:

SequenceProto

dictionary

onnx.numpy_helper.to_dict(map_proto: MapProto, base_dir: str | None = None) → dict[Any, Any][source]

Converts a map def to a Python dictionary.

Parameters:
  • map_proto – a MapProto object.

  • base_dir – directory containing external tensor data.

Returns:

The converted dictionary.

onnx.numpy_helper.from_dict(dict_: dict[Any, Any], name: str | None = None) → MapProto[source]

Converts a Python dictionary into a map def.

Integer, str and bytes keys are supported. str keys are stored UTF-8 encoded, so to_dict() returns them as bytes; such a dictionary can be passed back to this function unchanged.

Parameters:
  • dict – Python dictionary

  • name – (optional) the name of the map.

Returns:

the converted map def.

Return type:

MapProto

Raises:
  • ValueError – if the dictionary is empty, or a str key is not encodable as UTF-8.

  • TypeError – if the keys or the values do not all share one valid type.

optional

onnx.numpy_helper.to_optional(optional: OptionalProto, base_dir: str | None = None) → Any | None[source]

Converts an optional def to a Python optional.

Parameters:
  • optional – an OptionalProto object.

  • base_dir – directory containing external tensor data.

Returns:

the converted optional.

Return type:

opt

onnx.numpy_helper.from_optional(opt: Any | None, name: str | None = None, dtype: int | None = None) → OptionalProto[source]

Converts an optional value into a Optional def.

Parameters:
  • opt – a Python optional

  • name – (optional) the name of the optional.

  • dtype – (optional) type of element in the input, used for specifying optional values when converting empty none. dtype must be a valid OptionalProto.DataType value

Returns:

the converted optional def.

Return type:

optional