jax.numpy.select#

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

根据一系列条件选择值。

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

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

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

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

返回:

在每个位置,根据 condlist 中第一个 True 条目,从 choicelist 中选择的值组成的数组。

返回类型:

Array

另请参阅

示例

>>> 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() 实现。