maxframe.tensor.indices#
- maxframe.tensor.indices(dimensions, dtype=<class 'int'>, chunk_size=None)[源代码]#
返回一个表示网格索引的张量。
计算一个张量,其子张量包含索引值 0,1,...,仅在相应轴上变化。
- 参数:
- 返回:
grid -- 网格索引的张量,
grid.shape = (len(dimensions),) + tuple(dimensions)。- 返回类型:
Tensor
备注
输出形状是通过在维度元组前面加上维度数量得到的,即如果 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]提取所需元素会更直接。