Numpy Utility Functions

ray.rllib.utils.numpy.aligned_array(size: int, dtype, align: int = 64) numpy.ndarray[source]

Returns an array of a given size that is 64-byte aligned.

The returned array can be efficiently copied into GPU memory by TensorFlow.

  • size – The size (total number of items) of the array. For example, array([[0.0, 1.0], [2.0, 3.0]]) would have size=4.

  • dtype – The numpy dtype of the array.

  • align – The alignment to use.


A np.ndarray with the given specifications.

ray.rllib.utils.numpy.concat_aligned(items: List[numpy.ndarray], time_major: Optional[bool] = None) numpy.ndarray[source]

Concatenate arrays, ensuring the output is 64-byte aligned.

We only align float arrays; other arrays are concatenated as normal.

This should be used instead of np.concatenate() to improve performance when the output array is likely to be fed into TensorFlow.

  • items – The list of items to concatenate and align.

  • time_major – Whether the data in items is time-major, in which case, we will concatenate along axis=1.


The concat’d and aligned array.

ray.rllib.utils.numpy.convert_to_numpy(x: Union[Any, dict, tuple], reduce_type: bool = True, reduce_floats=- 1)[source]

Converts values in stats to non-Tensor numpy or python types.

  • x – Any (possibly nested) struct, the values in which will be converted and returned as a new struct with all torch/tf tensors being converted to numpy types.

  • reduce_type – Whether to automatically reduce all float64 and int64 data into float32 and int32 data, respectively.


A new struct with the same structure as x, but with all values converted to numpy arrays (on CPU).

ray.rllib.utils.numpy.fc(x: numpy.ndarray, weights: numpy.ndarray, biases: Optional[numpy.ndarray] = None, framework: Optional[str] = None) numpy.ndarray[source]

Calculates FC (dense) layer outputs given weights/biases and input.

  • x – The input to the dense layer.

  • weights – The weights matrix.

  • biases – The biases vector. All 0s if None.

  • framework – An optional framework hint (to figure out, e.g. whether to transpose torch weight matrices).


The dense layer’s output.

ray.rllib.utils.numpy.flatten_inputs_to_1d_tensor(inputs: Union[Any, dict, tuple], spaces_struct: Optional[Union[<MagicMock name='mock.spaces.Space' id='140325797319952'>, dict, tuple]] = None, time_axis: bool = False) Any[source]

Flattens arbitrary input structs according to the given spaces struct.

Returns a single 1D tensor resulting from the different input components’ values.

Thereby: - Boxes (any shape) get flattened to (B, [T]?, -1). Note that image boxes are not treated differently from other types of Boxes and get flattened as well. - Discrete (int) values are one-hot’d, e.g. a batch of [1, 0, 3] (B=3 with Discrete(4) space) results in [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1]]. - MultiDiscrete values are multi-one-hot’d, e.g. a batch of [[0, 2], [1, 4]] (B=2 with MultiDiscrete([2, 5]) space) results in [[1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 1]].

  • inputs – The inputs to be flattened.

  • spaces_struct – The structure of the spaces that behind the input

  • time_axis – Whether all inputs have a time-axis (after the batch axis). If True, will keep not only the batch axis (0th), but the time axis (1st) as-is and flatten everything from the 2nd axis up.


A single 1D tensor resulting from concatenating all flattened/one-hot’d input components. Depending on the time_axis flag, the shape is (B, n) or (B, T, n).


>>> # B=2
>>> from ray.rllib.utils.tf_utils import flatten_inputs_to_1d_tensor
>>> from gym.spaces import Discrete, Box
>>> out = flatten_inputs_to_1d_tensor( 
...     {"a": [1, 0], "b": [[[0.0], [0.1]], [1.0], [1.1]]},
...     spaces_struct=dict(a=Discrete(2), b=Box(shape=(2, 1)))
... ) 
>>> print(out) 
[[0.0, 1.0,  0.0, 0.1], [1.0, 0.0,  1.0, 1.1]]  # B=2 n=4
>>> # B=2; T=2
>>> out = flatten_inputs_to_1d_tensor( 
...     ([[1, 0], [0, 1]],
...      [[[0.0, 0.1], [1.0, 1.1]], [[2.0, 2.1], [3.0, 3.1]]]),
...     spaces_struct=tuple([Discrete(2), Box(shape=(2, ))]),
...     time_axis=True
... ) 
>>> print(out) 
[[[0.0, 1.0, 0.0, 0.1], [1.0, 0.0, 1.0, 1.1]],        [[1.0, 0.0, 2.0, 2.1], [0.0, 1.0, 3.0, 3.1]]]  # B=2 T=2 n=4

Flags actions immutable to notify users when trying to change them.

Can also be used with any tree-like structure containing either dictionaries, numpy arrays or already immutable objects per se. Note, however that tree.map_structure() will in general not include the shallow object containing all others and therefore immutability will hold only for all objects contained in it. Use tree.traverse(fun, action, top_down=False) to include also the containing object.


obj – The object to be made immutable.


The immutable object.


>>> import tree
>>> import numpy as np
>>> from ray.rllib.utils.numpy import make_action_immutable
>>> arr = np.arange(1,10)
>>> d = dict(a = 1, b = (arr, arr))
>>> tree.traverse(make_action_immutable, d, top_down=False) 
ray.rllib.utils.numpy.huber_loss(x: numpy.ndarray, delta: float = 1.0) numpy.ndarray[source]


ray.rllib.utils.numpy.l2_loss(x: numpy.ndarray) numpy.ndarray[source]

Computes half the L2 norm of a tensor (w/o the sqrt): sum(x**2) / 2.


x – The input tensor.


The l2-loss output according to the above formula given x.

ray.rllib.utils.numpy.lstm(x, weights: numpy.ndarray, biases: Optional[numpy.ndarray] = None, initial_internal_states: Optional[numpy.ndarray] = None, time_major: bool = False, forget_bias: float = 1.0)[source]

Calculates LSTM layer output given weights/biases, states, and input.

  • x – The inputs to the LSTM layer including time-rank (0th if time-major, else 1st) and the batch-rank (1st if time-major, else 0th).

  • weights – The weights matrix.

  • biases – The biases vector. All 0s if None.

  • initial_internal_states – The initial internal states to pass into the layer. All 0s if None.

  • time_major – Whether to use time-major or not. Default: False.

  • forget_bias – Gets added to first sigmoid (forget gate) output. Default: 1.0.


Tuple consisting of 1) The LSTM layer’s output and 2) Tuple: Last (c-state, h-state).

ray.rllib.utils.numpy.one_hot(x: Union[Any, int], depth: int = 0, on_value: float = 1.0, off_value: float = 0.0) numpy.ndarray[source]

One-hot utility function for numpy.

Thanks to qianyizhang:

  • 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.


The one-hot encoded equivalent of the input array.

ray.rllib.utils.numpy.relu(x: numpy.ndarray, alpha: float = 0.0) numpy.ndarray[source]

Implementation of the leaky ReLU function.

y = x * alpha if x < 0 else x

  • x – The input values.

  • alpha – A scaling (“leak”) factor to use for negative x.


The leaky ReLU output for x.

ray.rllib.utils.numpy.sigmoid(x: numpy.ndarray, derivative: bool = False) numpy.ndarray[source]

Returns the sigmoid function applied to x. Alternatively, can return the derivative or the sigmoid function.

  • x – The input to the sigmoid function.

  • derivative – Whether to return the derivative or not. Default: False.


The sigmoid function (or its derivative) applied to x.

ray.rllib.utils.numpy.softmax(x: Union[numpy.ndarray, list], axis: int = - 1, epsilon: Optional[float] = None) numpy.ndarray[source]

Returns the softmax values for x.

The exact formula used is: S(xi) = e^xi / SUMj(e^xj), where j goes over all elements in x.

  • x – The input to the softmax function.

  • axis – The axis along which to softmax.

  • epsilon – Optional epsilon as a minimum value. If None, use SMALL_NUMBER.


The softmax over x.