code: def from_array(
      arr: np.ndarray, name: Optional[str] = None
  ) -> TensorProto:
      """Converts a numpy array to a tensor def.
  
      Args:
          arr: a numpy array.
          name: (optional) the name of the tensor.
  
      Returns:
          TensorProto: the converted tensor def.
      """
      if not isinstance(arr, (np.ndarray, np.generic)):
          raise TypeError(
              f"arr must be of type np.generic or np.ndarray, got {type(arr)}"
          )
  
      tensor = TensorProto()
      tensor.dims.extend(arr.shape)
      if name:
          tensor.name = name
  
      if arr.dtype == object:
          # Special care for strings.
          tensor.data_type = helper.np_dtype_to_tensor_dtype(arr.dtype)
          # TODO: Introduce full string support.
          # We flatten the array in case there are 2-D arrays are specified
          # We throw the error below if we have a 3-D array or some kind of other
          # object. If you want more complex shapes then follow the below instructions.
          # Unlike other types where the shape is automatically inferred from
          # nested arrays of values, the only reliable way now to feed strings
          # is to put them into a flat array then specify type astype(object)
          # (otherwise all strings may have different types depending on their length)
          # and then specify shape .reshape([x, y, z])
          flat_array = arr.flatten()
          for e in flat_array:
              if isinstance(e, str):
                  tensor.string_data.append(e.encode("utf-8"))
              elif isinstance(e, np.ndarray):
                  for s in e:
                      if isinstance(s, str):
                          tensor.string_data.append(s.encode("utf-8"))
                      elif isinstance(s, bytes):
                          tensor.string_data.append(s)
              elif isinstance(e, bytes):
                  tensor.string_data.append(e)
              else:
                  raise NotImplementedError(
                      "Unrecognized object in the object array, expect a string, or array of bytes: ",
                      str(type(e)),
                  )
          return tensor
  
      # For numerical types, directly use numpy raw bytes.
      try:
          dtype = helper.np_dtype_to_tensor_dtype(arr.dtype)
      except KeyError as e:
          raise RuntimeError(
              f"Numpy data type not understood yet: {arr.dtype!r}"
          ) from e
      tensor.data_type = dtype
      tensor.raw_data = (
          arr.tobytes()
      )  # note: tobytes() is only after 1.9.
      if sys.byteorder == "big":
          # Convert endian from big to little
          convert_endian(tensor)
  
      return tensor
  
imports: import sys
  from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
  import numpy
  from onnx import MapProto, OptionalProto, SequenceProto, TensorProto, helper, subbyte
  from onnx.external_data_helper import load_external_data_for_tensor, uses_external_data
label_desc: Use image node_id 9 for calling a global function with example usage: from_array(arr, name) and returns: tensor, tensor