jax.numpy.select#

jax.numpy.select(condlist, choicelist, default=0)[source]#

根据一系列条件选择值。

JAX 对 numpy.select() 的实现,通过 jax.lax.select_n() 实现。

参数:
  • condlist (Sequence[ArrayLike]) – 类似数组的条件序列。所有条目必须相互兼容广播。

  • choicelist (Sequence[ArrayLike]) – 类似数组的值序列,用于选择。长度必须与 condlist 相同,并且所有条目必须与 condlist 中的条目兼容广播。

  • default (ArrayLike) – 当所有条件都为 False 时返回的值(默认值:0)。

返回:

choicelist 中选定值的数组,对应于每个位置 condlist 中的第一个 True 条目。

返回类型:

数组

另请参阅

示例

>>> condlist = [
...    jnp.array([False, True, False, False]),
...    jnp.array([True, False, False, False]),
...    jnp.array([False, True, True, False]),
... ]
>>> choicelist = [
...    jnp.array([1, 2, 3, 4]),
...    jnp.array([10, 20, 30, 40]),
...    jnp.array([100, 200, 300, 400]),
... ]
>>> jnp.select(condlist, choicelist, default=0)
Array([ 10,   2, 300,   0], dtype=int32)

这在逻辑上等同于以下嵌套的 where 语句

>>> default = 0
>>> jnp.where(condlist[0],
...   choicelist[0],
...   jnp.where(condlist[1],
...     choicelist[1],
...     jnp.where(condlist[2],
...       choicelist[2],
...       default)))
Array([ 10,   2, 300,   0], dtype=int32)

然而,为了效率,它是通过 jax.lax.select_n() 实现的。