maxframe.tensor.choose#

maxframe.tensor.choose(a, choices, out=None, mode='raise')[源代码]#

根据索引张量和一组可选张量构造一个新张量。

首先,如果感到困惑或不确定,请务必查看示例——从下面的代码描述来看(以下 ndi = mt.lib.index_tricks),此函数并不像表面上看起来那么简单:

mt.choose(a,c) == mt.array([c[a[I]][I] for I in ndi.ndindex(a.shape)]).

但这忽略了一些细节。以下是一个完全通用的总结:

给定一个整数“索引”张量 (a) 和 n 个张量的序列 (choices),a 和每个可选张量会根据需要进行广播,使其具有相同的形状;称这些广播后的张量为 BaBchoices[i], i = 0,...,n-1,则对于每个 i,必然有 Ba.shape == Bchoices[i].shape。然后,将创建一个形状为 Ba.shape 的新数组,如下所示:

  • 如果 ``mode=raise``(默认值),则首先,a`(因此也是 `Ba)的每个元素都必须在 [0, n-1] 范围内;现在,假设 i`(在该范围内)是 `Ba(j0, j1, ..., jm) 位置的值——那么新数组中相同位置的值就是在 Bchoices[i] 中相同位置的值;

  • 如果 mode=wrapa`(因此也是 `Ba)中的值可以是任意(有符号)整数;使用模运算将范围外的整数映射回 [0, n-1] 范围内;然后按上述方式构造新数组;

  • 如果 mode=clipa`(因此也是 `Ba)中的值可以是任意(有符号)整数;负整数被映射为 0;大于 n-1 的值被映射为 n-1;然后按上述方式构造新张量。

参数:
  • a (int tensor) -- 此张量必须包含 [0, n-1] 范围内的整数,其中 n 是可选项的数量,除非 mode=wrapmode=clip,在这种情况下允许任意整数。

  • choices (sequence of tensors) -- 可选张量。a 和所有可选项必须能够广播到相同的形状。如果 choices 本身是张量(不推荐),则其最外层维度(即对应于 choices.shape[0] 的维度)被视为定义“序列”的维度。

  • out (tensor, optional) -- 如果提供此参数,结果将被插入到该张量中。它应具有适当的形状和数据类型。

  • mode ({'raise' (default), 'wrap', 'clip'}, optional) -- 指定如何处理 [0, n-1] 范围之外的索引: * 'raise' : 引发异常 * 'wrap' : 值变为值模 n * 'clip' : 值 < 0 被映射为 0,值 > n-1 被映射为 n-1

返回:

merged_array -- 合并后的结果。

返回类型:

Tensor

抛出:

ValueError -- 形状不匹配:如果 a 和每个可选张量不能全部广播到相同的形状。

参见

Tensor.choose

等效方法

备注

为了减少误解的可能性,尽管以下“滥用”在名义上是支持的,但 choices 既不应是一个张量,也不应被视为一个张量,即最外层的序列类容器应为列表或元组。

示例

>>> import maxframe.tensor as mt
>>> choices = [[0, 1, 2, 3], [10, 11, 12, 13],
...   [20, 21, 22, 23], [30, 31, 32, 33]]
>>> mt.choose([2, 3, 1, 0], choices
... # the first element of the result will be the first element of the
... # third (2+1) "array" in choices, namely, 20; the second element
... # will be the second element of the fourth (3+1) choice array, i.e.,
... # 31, etc.
... ).execute()
array([20, 31, 12,  3])
>>> mt.choose([2, 4, 1, 0], choices, mode='clip').execute() # 4 goes to 3 (4-1)
array([20, 31, 12,  3])
>>> # because there are 4 choice arrays
>>> mt.choose([2, 4, 1, 0], choices, mode='wrap').execute() # 4 goes to (4 mod 4)
array([20,  1, 12,  3])
>>> # i.e., 0

几个说明 choose 如何进行广播的示例:

>>> a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
>>> choices = [-10, 10]
>>> mt.choose(a, choices).execute()
array([[ 10, -10,  10],
       [-10,  10, -10],
       [ 10, -10,  10]])
>>> # With thanks to Anne Archibald
>>> a = mt.array([0, 1]).reshape((2,1,1))
>>> c1 = mt.array([1, 2, 3]).reshape((1,3,1))
>>> c2 = mt.array([-1, -2, -3, -4, -5]).reshape((1,1,5))
>>> mt.choose(a, (c1, c2)).execute() # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2
array([[[ 1,  1,  1,  1,  1],
        [ 2,  2,  2,  2,  2],
        [ 3,  3,  3,  3,  3]],
       [[-1, -2, -3, -4, -5],
        [-1, -2, -3, -4, -5],
        [-1, -2, -3, -4, -5]]])