maxframe.tensor.indices#

maxframe.tensor.indices(dimensions, dtype=<class 'int'>, chunk_size=None)[源代码]#

返回一个表示网格索引的张量。

计算一个张量,其子张量包含索引值 0,1,...,仅在相应轴上变化。

参数:
  • dimensions (sequence of ints) -- 网格的形状。

  • dtype (dtype, optional) -- 结果的数据类型。

  • chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的块大小

返回:

grid -- 网格索引的张量,grid.shape = (len(dimensions),) + tuple(dimensions)

返回类型:

Tensor

参见

mgrid, meshgrid

备注

输出形状是通过在维度元组前面加上维度数量得到的,即如果 dimensions 是长度为 N 的元组 (r0, ..., rN-1),则输出形状为 (N,r0,...,rN-1)

子张量 grid[k] 包含沿第 k 个轴的 N 维索引数组。显式地:

grid[k,i0,i1,...,iN-1] = ik

示例

>>> import maxframe.tensor as mt
>>> grid = mt.indices((2, 3))
>>> grid.shape
(2, 2, 3)
>>> grid[0].execute()        # row indices
array([[0, 0, 0],
       [1, 1, 1]])
>>> grid[1].execute()        # column indices
array([[0, 1, 2],
       [0, 1, 2]])

这些索引可以用作张量的索引。

>>> x = mt.arange(20).reshape(5, 4)
>>> row, col = mt.indices((2, 3))
>>> # x[row, col]

注意,在上面的示例中,直接使用 x[:2, :3] 提取所需元素会更直接。