rllib/utils/tf_utils.py
def one_hot(x: TensorType, space: gym.Space) -> TensorType:
    """Returns a one-hot tensor, given and int tensor and a space.

    Handles the MultiDiscrete case as well.

    Args:
        x: The input tensor.
        space: The space to use for generating the one-hot tensor.

    Returns:
        The resulting one-hot tensor.

    Raises:
        ValueError: If the given space is not a discrete one.

    Examples:
        >>> import gym
        >>> import tensorflow as tf
        >>> from ray.rllib.utils.tf_utils import one_hot
        >>> x = tf.Variable([0, 3], dtype=tf.int32)  # batch-dim=2
        >>> # Discrete space with 4 (one-hot) slots per batch item.
        >>> s = gym.spaces.Discrete(4)
        >>> one_hot(x, s) # doctest: +SKIP
        <tf.Tensor 'one_hot:0' shape=(2, 4) dtype=float32>
        >>> x = tf.Variable([[0, 1, 2, 3]], dtype=tf.int32)  # batch-dim=1
        >>> # MultiDiscrete space with 5 + 4 + 4 + 7 = 20 (one-hot) slots
        >>> # per batch item.
        >>> s = gym.spaces.MultiDiscrete([5, 4, 4, 7])
        >>> one_hot(x, s) # doctest: +SKIP
        <tf.Tensor 'concat:0' shape=(1, 20) dtype=float32>
    """
    if isinstance(space, Discrete):
        return tf.one_hot(x, space.n, dtype=tf.float32)
    elif isinstance(space, MultiDiscrete):
        return tf.concat(
            [
                tf.one_hot(x[:, i], n, dtype=tf.float32)
                for i, n in enumerate(space.nvec)
            ],
            axis=-1,
        )
    else:
        raise ValueError("Unsupported space for `one_hot`: {}".format(space))
Returns a one-hot tensor, given and int tensor and a space.

    Handles the MultiDiscrete case as well.

    Args:
        x: The input tensor.
        space: The space to use for generating the one-hot tensor.

    Returns:
        The resulting one-hot tensor.

    Raises:
        ValueError: If the given space is not a discrete one.

    Examples:
        >>> import gym
        >>> import tensorflow as tf
        >>> from ray.rllib.utils.tf_utils import one_hot
        >>> x = tf.Variable([0, 3], dtype=tf.int32)  # batch-dim=2
        >>> # Discrete space with 4 (one-hot) slots per batch item.
        >>> s = gym.spaces.Discrete(4)
        >>> one_hot(x, s) # doctest: +SKIP
        <tf.Tensor 'one_hot:0' shape=(2, 4) dtype=float32>
        >>> x = tf.Variable([[0, 1, 2, 3]], dtype=tf.int32)  # batch-dim=1
        >>> # MultiDiscrete space with 5 + 4 + 4 + 7 = 20 (one-hot) slots
        >>> # per batch item.
        >>> s = gym.spaces.MultiDiscrete([5, 4, 4, 7])
        >>> one_hot(x, s) # doctest: +SKIP
        <tf.Tensor 'concat:0' shape=(1, 20) dtype=float32>
    
Returns a one-hot tensor, given and int tensor and a space.
Handles the MultiDiscrete case as well.
Returns a one - hot tensor given and int tensor and a space . Handles the MultiDiscrete case as well .
def one_hot(x: TensorType, space: gym.Space) -> TensorType:
    if isinstance(space, Discrete):
        return tf.one_hot(x, space.n, dtype=tf.float32)
    elif isinstance(space, MultiDiscrete):
        return tf.concat(
            [
                tf.one_hot(x[:, i], n, dtype=tf.float32)
                for i, n in enumerate(space.nvec)
            ],
            axis=-1,
        )
    else:
        raise ValueError("Unsupported space for `one_hot`: {}".format(space))
def one_hot ( x : TensorType , space : gym . Space ) -> TensorType : if isinstance ( space , Discrete ) : return tf . one_hot ( x , space . n , dtype = tf . float32 ) elif isinstance ( space , MultiDiscrete ) : return tf . concat ( [ tf . one_hot ( x [ : , i ] , n , dtype = tf . float32 ) for i , n in enumerate ( space . nvec ) ] , axis = - 1 , ) else : raise ValueError ( "Unsupported space for `one_hot`: {}" . format ( space ) )
Returns a one-hot tensor, given and int tensor and a space.
Returns a one - hot tensor given and int tensor and a space .
"""Returns a one-hot tensor, given and int tensor and a space.

    Handles the MultiDiscrete case as well.

    Args:
        x: The input tensor.
        space: The space to use for generating the one-hot tensor.

    Returns:
        The resulting one-hot tensor.

    Raises:
        ValueError: If the given space is not a discrete one.

    Examples:
        >>> import gym
        >>> import tensorflow as tf
        >>> from ray.rllib.utils.tf_utils import one_hot
        >>> x = tf.Variable([0, 3], dtype=tf.int32)  # batch-dim=2
        >>> # Discrete space with 4 (one-hot) slots per batch item.
        >>> s = gym.spaces.Discrete(4)
        >>> one_hot(x, s) # doctest: +SKIP
        <tf.Tensor 'one_hot:0' shape=(2, 4) dtype=float32>
        >>> x = tf.Variable([[0, 1, 2, 3]], dtype=tf.int32)  # batch-dim=1
        >>> # MultiDiscrete space with 5 + 4 + 4 + 7 = 20 (one-hot) slots
        >>> # per batch item.
        >>> s = gym.spaces.MultiDiscrete([5, 4, 4, 7])
        >>> one_hot(x, s) # doctest: +SKIP
        <tf.Tensor 'concat:0' shape=(1, 20) dtype=float32>
    """
The resulting one-hot tensor.
The resulting one - hot tensor .
If the given space is not a discrete one.
If the given space is not a discrete one .
ValueError
x
TensorType
The input tensor.
The input tensor .
space
gym.Space
The space to use for generating the one-hot tensor.
The space to use for generating the one - hot tensor .
examples
>>> import gym
>>> import tensorflow as tf
>>> from ray.rllib.utils.tf_utils import one_hot
>>> x = tf.Variable([0, 3], dtype=tf.int32)  # batch-dim=2
>>> # Discrete space with 4 (one-hot) slots per batch item.
>>> import gym >>> import tensorflow as tf >>> from ray . rllib . utils . tf_utils import one_hot >>> x = tf . Variable ( [ 0 3 ] dtype = tf . int32 ) # batch - dim = 2 >>> # Discrete space with 4 ( one - hot ) slots per batch item .