jax.numpy.select

目录

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) – 当所有条件都为假时返回的值(默认值:0)。

返回:

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

返回类型:

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