jax.numpy.empty_like#

jax.numpy.empty_like(prototype, dtype=None, shape=None, *, device=None)[source]#

Create an empty array with the same shape and dtype as an array.

JAX implementation of numpy.empty_like(). Because XLA cannot create an un-initialized array, jax.numpy.empty() will always return an array full of zeros.

Parameters:
  • a – Array-like object with shape and dtype attributes.

  • shape (Any | None) – optionally override the shape of the created array.

  • dtype (DTypeLike | None | None) – optionally override the dtype of the created array.

  • device (xc.Device | Sharding | None | None) – (optional) Device or Sharding to which the created array will be committed.

  • prototype (ArrayLike | DuckTypedArray)

Returns:

Array of the specified shape and dtype, on the specified device if specified.

Return type:

Array

Examples

>>> x = jnp.arange(4)
>>> jnp.empty_like(x)
Array([0, 0, 0, 0], dtype=int32)
>>> jnp.empty_like(x, dtype=bool)
Array([False, False, False, False], dtype=bool)
>>> jnp.empty_like(x, shape=(2, 3))
Array([[0, 0, 0],
       [0, 0, 0]], dtype=int32)