code: def one_hot(
      x: Union[TensorType, int],
      depth: int = 0,
      on_value: float = 1.0,
      off_value: float = 0.0,
      dtype: type = np.float32,
  ) -> np.ndarray:
      """One-hot utility function for numpy.
  
      Thanks to qianyizhang:
      https://gist.github.com/qianyizhang/07ee1c15cad08afb03f5de69349efc30.
  
      Args:
          x: The input to be one-hot encoded.
          depth: The max. number to be one-hot encoded (size of last rank).
          on_value: The value to use for on. Default: 1.0.
          off_value: The value to use for off. Default: 0.0.
  
      Returns:
          The one-hot encoded equivalent of the input array.
      """
  
      # Handle simple ints properly.
      if isinstance(x, int):
          x = np.array(x, dtype=np.int32)
      # Handle torch arrays properly.
      elif torch and isinstance(x, torch.Tensor):
          x = x.numpy()
  
      # Handle bool arrays correctly.
      if x.dtype == np.bool_:
          x = x.astype(np.int_)
          depth = 2
  
      # If depth is not given, try to infer it from the values in the array.
      if depth == 0:
          depth = np.max(x) + 1
      assert (
          np.max(x) < depth
      ), "ERROR: The max. index of `x` ({}) is larger than depth ({})!".format(
          np.max(x), depth
      )
      shape = x.shape
  
      out = np.ones(shape=(*shape, depth)) * off_value
      indices = []
      for i in range(x.ndim):
          tiles = [1] * x.ndim
          s = [1] * x.ndim
          s[i] = -1
          r = np.arange(shape[i]).reshape(s)
          if i > 0:
              tiles[i - 1] = shape[i - 1]
              r = np.tile(r, tiles)
          indices.append(r)
      indices.append(x)
      out[tuple(indices)] = on_value
      return out.astype(dtype)
  
imports: from collections import OrderedDict
  from gymnasium.spaces import Discrete, MultiDiscrete
  import numpy
  import tree
  from types import MappingProxyType
  from typing import List, Optional
  from ray.rllib.utils.annotations import PublicAPI
  from ray.rllib.utils.deprecation import Deprecated
  from ray.rllib.utils.framework import try_import_tf, try_import_torch
  from ray.rllib.utils.typing import SpaceStruct, TensorType, TensorStructType, Union
label_desc: Use image node_id 10 for calling a global function with example usage: one_hot(x, depth, on_value, off_value, dtype) and returns: out