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条目。- 返回类型:
参见
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()实现的。