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中选择的值组成的数组。- 返回类型:
另请参阅
jax.numpy.where():根据单个条件在两个值之间进行选择。jax.lax.select_n():根据索引在 *N* 个值之间进行选择。
示例
>>> 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()实现。