Tensor Index Support
Single-level and multi-level Tensor indexing is supported on both PyNative and Graph mode.
Index values
The index value can be int, bool, None, ellipsis, slice, Tensor, List, or Tuple.
intindex valueSingle-level and multi-level
intindex values are supported. The single-levelintindex value istensor_x[int_index], and the multi-levelintindex value istensor_x[int_index0][int_index1]....The
intindex value is obtained on dimension 0 and is less than the length of dimension 0. After the position data corresponding to dimension 0 is obtained, dimension 0 is eliminated.For example, if a single-level
intindex value is obtained for a tensor whoseshapeis(3, 4, 5), the obtainedshapeis(4, 5).The multi-level index value can be understood as obtaining the current-level
intindex value based on the previous-level index value.For example:
from mindspore import Tensor import mindspore.numpy as np tensor_x = Tensor(np.arange(2 * 3 * 2).reshape((2, 3, 2))) data_single = tensor_x[0] data_multi = tensor_x[0][1]
The result is as follows:
data_single: Tensor(shape=[3, 2], dtype=Int64, value=[[0, 1], [2, 3], [4, 5]]) data_multi: Tensor(shape=[2], dtype=Int64, value=[2, 3])
boolindex valueSingle-level and multi-level
boolindex values are supported. The single-levelboolindex value istensor_x[True], and the multi-levelTrueindex value istensor_x[True][True]....The
Trueindex value operation is obtained on dimension 0. After all data is obtained, a dimension is extended on theaxis=0axis. The length of the dimension is 1.Falsewill introduce0in the shape, thus onlyTureis supported now.For example, if a single-level
Trueindex value is obtained from a tensor whoseshapeis(3, 4, 5), the obtainedshapeis(1, 3, 4, 5).The multi-level index value can be understood as obtaining the current-level
boolindex value based on the previous-level index value.For example:
tensor_x = Tensor(np.arange(2 * 3 ).reshape((2, 3))) data_single = tensor_x[True] data_multi = tensor_x[True][True]
The result is as follows:
data_single: Tensor(shape=[1, 2, 3], dtype=Int64, value=[[[0, 1, 2], [3, 4, 5]]]) data_multi: Tensor(shape=[1, 0, 2, 3], dtype=Int64, value=[[[[0, 1, 2], [3 , 4, 5]]]])
Noneindex valueThe
Noneindex value is the same as theTrueindex value. For details, see theTrueindex value.ellipsisindex valueSingle-level and multi-level
ellipsisindex values are supported. The single-levelellipsisindex value istensor_x[...], and the multi-levelellipsisindex value istensor_x[...][...]....The
ellipsisindex value is obtained on all dimensions to get the original data without any change. Generally, it is used as a component of theTupleindex. TheTupleindex is described as follows.For example, if the
ellipsisindex value is obtained for a tensor whoseshapeis(3, 4, 5), the obtainedshapeis still(3, 4, 5).For example:
tensor_x = Tensor(np.arange(2 * 3 ).reshape((2, 3))) data_single = tensor_x[...] data_multi = tensor_x[...][...]
The result is as follows:
data_single: Tensor(shape=[2, 3], dtype=Int64, value=[[0, 1, 2], [3, 4, 5]]) data_multi: Tensor(shape=[2, 3], dtype=Int64, value=[[0, 1, 2], [3, 4, 5]])
sliceindex valueSingle-level and multi-level
sliceindex values are supported. The single-levelsliceindex value istensor_x[slice_index], and the multi-levelsliceindex value istensor_x[slice_index0][slice_index1]....The
sliceindex value is obtained on dimension 0. The element of the sliced position on dimension 0 is obtained. Theslicedoes not reduce the dimension even if the length is 1, which is different from theintindex value.For example,
tensor_x[0:1:1] != tensor_x[0], becauseshape_former = (1,) + shape_latter.The multi-level index value can be understood as obtaining the current-level
sliceindex value based on the previous-level index value.sliceconsists ofstart,stop, andstep. The default value ofstartis 0, the default value ofstopis the length of the dimension, and the default value ofstepis 1.Example:
tensor_x[:] == tensor_x[0:length:1].For example:
tensor_x = Tensor(np.arange(4 * 2 * 2).reshape((4, 2, 2))) data_single = tensor_x[1:4:2] data_multi = tensor_x[1:4:2][1:]
The result is as follows:
data_single: Tensor(shape=[2, 2, 2], dtype=Int64, value=[[[4, 5], [6, 7]], [[12, 13], [14, 15]]]) data_multi: Tensor(shape=[1, 2, 2], dtype=Int64, value=[[[12, 13], [14, 15]]])
Tensorindex valueSingle-level and multi-level
Tensorindex values are supported. The single-levelTensorindex value istensor_x[tensor_index], and the multi-levelTensorindex value istensor_x[tensor_index0][tensor_index1]....The
Tensorindex value is obtained on dimension 0, and the element in the corresponding position of dimension 0 is obtained.The data type of the
Tensorindex must be one of int8, int16, int32, and int64, the element cannot be a negative number, and the value must be less than the length of dimension 0.The
Tensorindex value is obtained bydata_shape = tensor_inde4x.shape + tensor_x.shape[1:].For example, if the index value is obtained for a tensor whose shape is
(6, 4, 5)by using a tensor whose shape is(2, 3), the obtained shape is(2, 3, 4, 5).The multi-level index value can be understood as obtaining the current-level
Tensorindex value based on the previous-level index value.For example:
tensor_x = Tensor(np.arange(4 * 2 * 3).reshape((4, 2, 3))) tensor_index0 = Tensor(np.array([[1, 2], [0, 3]]), mstype.int32) tensor_index1 = Tensor(np.array([[0, 0]]), mstype.int32) data_single = tensor_x[tensor_index0] data_multi = tensor_x[tensor_index0][tensor_index1]
The result is as follows:
data_single: Tensor(shape=[2, 2, 2, 3], dtype=Int64, value=[[[[6, 7, 8], [9, 10, 11]], [[12, 13, 14], [15, 16 ,17]]], [[[0, 1, 2], [3, 4, 5]], [[18, 19, 20], [21, 22, 23]]]]) data_multi: Tensor(shape=[1, 2, 2, 2, 3], dtype=Int64, value=[[[[[6, 7, 8], [9, 10, 11]], [[12, 13, 14], [15, 16 ,17]]], [[[0, 1, 2], [3, 4, 5]], [[18, 19, 20], [21, 22, 23]]]]]))
Listindex valueSingle-level and multi-level
Tensorindex values are supported. The single-levelListindex value istensor_x[list_index], and the multi-levelListindex value istensor_x[list_index0][list_index1]....The
Listindex value is obtained on dimension 0, and the element in the corresponding position of dimension 0 is obtained.The data type of the
Listindex must be all bool, all int or mixed of them. TheListelements of int type must be in the range of [-dimension_shape,dimension_shape-1] and the count ofListelements with bool type must be the same as thedimension_shapeof dimension 0 and will perform as to filter the corresponding element of the Tenson data. If the above two types appear simultaneously, theListelements with the bool type will be converted to1/0forTrue/False.The
Tensorindex value is obtained bydata_shape = tensor_inde4x.shape + tensor_x.shape[1:].For example, if the index value is obtained for a tensor whose shape is
(6, 4, 5)by using a tensor whose shape is(2, 3), the obtained shape is(2, 3, 4, 5).The multi-level index value can be understood as obtaining the current-level
Tensorindex value based on the previous-level index value.For example:
tensor_x = Tensor(np.arange(4 * 2 * 3).reshape((4, 2, 3))) tensor_index0 = Tensor(np.array([[1, 2], [0, 3]]), mstype.int32) tensor_index1 = Tensor(np.array([[0, 0]]), mstype.int32) data_single = tensor_x[tensor_index0] data_multi = tensor_x[tensor_index0][tensor_index1]
The result is as follows:
data_single: Tensor(shape=[2, 2, 2, 3], dtype=Int64, value=[[[[4, 5], [6, 7]], [[8, 9], [10, 11]]], [[[0, 1], [2, 3]], [[12, 13], [14, 15]]]]) data_multi: Tensor(shape=[1, 2, 2, 2, 3], dtype=Int64, value=[[[[[4, 5], [6, 7]], [[8, 9], [10, 11]]], [[[4, 5], [6, 7]], [[8, 9], [10, 11]]]]])
Tupleindex valueThe data type of the
Tupleindex can beint,bool,None,slice,ellipsis,Tensor,List, orTuple. Single-level and multi-levelTupleindex values are supported. For the single-levelTupleindex, the value istensor_x[tuple_index]. For the multi-levelTupleindex, the value istensor_x[tuple_index0][tuple_index1].... The regulations of elementsListandTupleare the same as that of single indexListindex. The regulations of others are the same to the responding single element type.Elements in the
Tupleindex can be sort out byBasic IndexorAdvanced Index.slice,ellipsisandNoneareBasic Indexandint,bool,Tensor,List,TupleareAdvanced Index. In the Getitem Progress, all the elements of theAdvanced Indextype will be broadcast to the same shape, and the final shape will be inserted to the firstAdvanced Indexelement’s position if they are continuous, else they will be inserted to the0position.In the index, the
Noneelements will expand the corresponding dimensions,boolelements will expand the corresponding dimension and be broadcast with the otherAdvanced Indexelement. The others elements except the type ofellipsis,bool, andNone, will correspond to each position dimension. That is, the 0th element inTupleoperates the 0th dimension, and the 1st element operates the 1st dimension. The index rule of each element is the same as the index value rule of the element type.The
Tupleindex contains a maximum of oneellipsis. The first half of theellipsisindex elements correspond to theTensordimensions starting from the dimension 0, and the second half of the index elements correspond to theTensordimensions starting from the last dimension. If other dimensions are not specified, all dimensions are obtained.The data type of
Tensorcontained in the element must be one of (int8, int16, int32, int64). In addition, the value ofTensorelement must be non-negative and less than the length of the operation dimension.For example,
tensor_x[0:3, 1, tensor_index] == tensor_x[(0:3, 1, tensor_index)], because0:3, 1, tensor_indexis aTuple.The multi-level index value can be understood as obtaining the current-level
Tupleindex value based on the previous-level index value.For example:
tensor_x = Tensor(np.arange(2 * 3 * 4).reshape((2, 3, 4))) tensor_index = Tensor(np.array([[1, 2, 1], [0, 3, 2]]), mstype.int32) data = tensor_x[1, 0:1, tensor_index]
The result is as follows:
data: Tensor(shape=[2, 3, 1], dtype=Int64, value=[[[13], [14], [13]], [[12], [15], [14]]])
Index value assignment
For a case like: tensor_x[index] = value, the type of the index can be int, bool, ellipsis, slice, None, Tensor, List, orTuple.
The type of the assigned value can be Number, Tuple, List, or Tensor, the value will be converted to Tensor and casted to the same dtype as the original tensor (tensor_x) before being assigned.
When value is Number, all position elements obtained from the tensor_x[index] will be updated to Number.
When value is a tensor whose type is Tuple, List or Tensor and only contains Number, the value.shape needs to be able to be broadcasted to tensor_x[index].shape. After the value’ is broadcasted and casted to Tensor, the elements with the position tensor_x[index] will be updated with the value broadcast(Tensor(value)).
When value is Tuple/List, and contains mixtures of Number, Tuple, List and Tensor, only one-dimensional Tuple and List are currently supported.
When value is Tuple or List, and contains Tensor, all the non-Tensor elements in value will be converted to Tensor first, and then these Tensor values are packed on the axis=0 axis and become new Tensor. In this case, the value is assigned according to the rule of assigning the value to Tensor. All Tensors must have the same dtype.
Index value assignment can be understood as assigning values to indexed position elements based on certain rules. All index value assignment does not change the original shape of Tensor.
If there are multiple index elements in indices that correspond to the same position, the value of that position in the output will be nondeterministic. For more details, please see:TensorScatterUpdate
Only single-bracket indexing is supported (
tensor_x[index] = value), multi-bracket(tensor_x[index1][index2]... = value) is not supported.
intindex value assignmentSingle-level
intindex value assignments are supported. The single-levelintindex value assignment istensor_x[int_index] = u.For example:
import mindspore.numpy as np tensor_x = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_y = np.arange(2 *3).reshape((2, 3)).astype(np.float32) tensor_z = np.arange(2* 3).reshape((2, 3)).astype(np.float32) tensor_x[1] = 88.0 tensor_y[1]= np.array([66, 88, 99]).astype(np.float32) tensor_z[1] = (66, np.array(88).astype(np.int64), 99)
The result is as follows:
tensor_x: Tensor(shape=[2, 3], dtype=Float32, value=[[0.0, 1.0, 2.0], [88.0, 88.0, 88.0]]) tensor_y: Tensor(shape=[2, 3], dtype=Float32, value=[[0.0, 1.0, 2.0], [66.0, 88.0, 99.0]]) tensor_z: Tensor(shape=[2, 3], dtype=Float32, value=[[0.0, 1.0, 2.0], [66.0, 88.0, 99.0]])
boolindex value assignmentSingle-level
boolindex value assignments are supported. The single-levelintindex value assignment istensor_x[bool_index] = u.For example:
import mindspore.numpy as np tensor_x = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_y = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_z = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_x[True] = 88.0 tensor_y[True]= np.array([66, 88, 99]).astype(np.float32) tensor_z[True] = (66, 88, 99)
The result is as follows:
tensor_x: Tensor(shape=[2, 3], dtype=Float32, value=[[88.0, 88.0, 88.0], [88.0, 88.0, 88.0]]) tensor_y: Tensor(shape=[2, 3], dtype=Float32, value=[[66.0, 88.0, 99.0], [66.0, 88.0, 99.0]]) tensor_z: Tensor(shape=[2, 3], dtype=Float32, value=[[66.0, 88.0, 99.0], [66.0, 88.0, 99.0]])
ellipsisindex value assignmentSingle-level
ellipsisindex value assignments are supported. The single-levelellipsisindex value assignment istensor_x[...] = u.For example:
import mindspore.numpy as np tensor_x = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_y = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_z = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_x[...] = 88 tensor_y[...]= np.array([[22, 44, 55], [22, 44, 55]]) tensor_z[...] = ([11, 22, 33], [44, 55, 66])
The result is as follows:
tensor_x: Tensor(shape=[2, 3], dtype=Float32, value=[[88.0, 88.0, 88.0], [88.0, 88.0, 88.0]]) tensor_y: Tensor(shape=[2, 3], dtype=Float32, value=[[22.0, 44.0, 55.0], [22.0, 44.0, 55.0]]) tensor_z: Tensor(shape=[2, 3], dtype=Float32, value=[[11.0, 22.0, 33.0], [44.0, 55.0, 66.0]])
sliceindex value assignmentSingle-level
sliceindex value assignments are supported. The single-levelsliceindex value assignment istensor_x[slice_index] = u.For example:
import mindspore.numpy as np tensor_x = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_y = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_z = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_k = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_x[0:1] = 88.0 tensor_y[0:2] = 88.0 tensor_z[0:2] = np.array([[11, 12, 13], [11, 12, 13]]).astype(np.float32) tensor_k[0:2] = ([11, 12, 13], (14, 15, 16))
The result is as follows:
tensor_x: Tensor(shape=[3, 3], dtype=Float32, value=[[88.0, 88.0, 88.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]) tensor_y: Tensor(shape=[3, 3], dtype=Float32, value=[[88.0, 88.0, 88.0], [88.0, 88.0, 88.0], [6.0, 7.0, 8.0]]) tensor_z: Tensor(shape=[3, 3], dtype=Float32, value=[[11.0, 12.0, 13.0], [11.0, 12.0, 13.0], [6.0, 7.0, 8.0]]) tensor_k: Tensor(shape=[3, 3], dtype=Float32, value=[[11.0, 12.0, 13.0], [14.0, 15.0, 16.0], [6.0, 7.0, 8.0]])
Noneindex value assignmentSingle-level
Noneindex value assignments are supported. The single-levelintindex value assignment istensor_x[none_index] = u.For example:
import mindspore.numpy as np tensor_x = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_y = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_z = np.arange(2 * 3).reshape((2, 3)).astype(np.float32) tensor_x[None] = 88.0 tensor_y[None]= np.array([66, 88, 99]).astype(np.float32) tensor_z[None] = (66, 88, 99)
The result is as follows:
tensor_x: Tensor(shape=[2, 3], dtype=Float32, value=[[88.0, 88.0, 88.0], [88.0, 88.0, 88.0]]) tensor_y: Tensor(shape=[2, 3], dtype=Float32, value=[[66.0, 88.0, 99.0], [66.0, 88.0, 99.0]]) tensor_z: Tensor(shape=[2, 3], dtype=Float32, value=[[66.0, 88.0, 99.0], [66.0, 88.0, 99.0]])
Tensorindex value assignmentSingle-level
Tensorindex value assignments are supported. The single-levelTensorindex value assignment istensor_x[tensor_index] = u.Boolean
Tensorindex is not currently supported, onlymstype.int*type is supported.For example:
import mindspore.numpy as np tensor_x = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_y = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_z = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_index = np.array([[2, 0, 2], [0, 2, 0], [0, 2, 0]], np.int32) tensor_x[tensor_index] = 88.0 tensor_y[tensor_index] = np.array([11.0, 12.0, 13.0]).astype(np.float32) tensor_z[tensor_index] = [11, 12, 13]
The result is as follows:
tensor_x: Tensor(shape=[3, 3], dtype=Float32, value=[[88.0, 88.0, 88.0], [3.0, 4.0, 5.0], [88.0, 88.0, 88.0]]) tensor_y: Tensor(shape=[3, 3], dtype=Float32, value=[[11.0, 12.0, 13.0], [3.0, 4.0, 5.0], [11.0, 12.0, 13.0]]) tensor_z: Tensor(shape=[3, 3], dtype=Float32, value=[[11.0, 12.0, 13.0], [3.0, 4.0, 5.0], [11.0, 12.0, 13.0]])
Listindex value assignmentsingle-level
Listindex value assignments are supported. The single-levelListindex value assignment istensor_x[list_index] = u.The
Listindex value assignment is the same as that of theListindex value.For example:
import mindspore.numpy as np tensor_x = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_y = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_index = np.array([[0, 1], [1, 0]]).astype(np.int32) tensor_x[[0,1]] = 88.0 tensor_y[[True, False, False]] = np.array([11, 12, 13]).astype(np.float32)
The result is as follows:
tensor_x: Tensor(shape=[3, 3], dtype=Float32, value=[[88.0, 88.0, 88.0], [88.0, 88.0, 88.0], [6.0, 7.0, 8.0]]) tensor_y: Tensor(shape=[3, 3], dtype=Float32, value=[[11.0, 12.0, 13.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])
Tupleindex value assignmentsingle-level
Tupleindex value assignments are supported. The single-levelTupleindex value assignment istensor_x[tuple_index] = u.The
Tupleindex value assignment is the same as that of theTupleindex value, butNonetype is not supported now.For example:
import mindspore.numpy as np tensor_x = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_y = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_z = np.arange(3 * 3).reshape((3, 3)).astype(np.float32) tensor_index = np.array([0, 1]).astype(np.int32) tensor_x[1, 1:3] = 88.0 tensor_y[1:3, tensor_index] = 88.0 tensor_z[1:3, tensor_index] = np.array([11, 12]).astype(np.float32)
The result is as follows:
tensor_x: Tensor(shape=[3, 3], dtype=Float32, value=[[0.0, 1.0, 2.0], [3.0, 88.0, 88.0], [6.0, 7.0, 8.0]]) tensor_y: Tensor(shape=[3, 3], dtype=Float32, value=[[0.0, 1.0, 2.0], [88.0, 88.0, 5.0], [88.0, 88.0, 8.0]]) tensor_z: Tensor(shape=[3, 3], dtype=Float32, value=[[0.0, 1.0, 2.0], [11.0, 12.0, 5.0], [11.0, 12.0, 8.0]])
Index value augmented-assignment
Index value augmented-assignment supports seven augmented_assignment operations: +=, -=, *=, /=, %=, **=, and //=. The rules and constraints of index and value are the same as index assignment. The index value supports eight types: int, bool, ellipsis, slice, None, tensor, list and tuple. The assignment value supports four types: Number, Tensor, Tuple and List.
Index value augmented-assignment can be regarded as taking the value of the position elements to be indexed according to certain rules, and then performing operator operation with value. Finally, assign the operation result to the origin Tensor. All index augmented-assignments will not change the shape of the original Tensor.
If there are multiple index elements in indices that correspond to the same position, the value of that position in the output will be nondeterministic. For more details, please see:TensorScatterUpdate
Currently indices that contain
True,FalseandNoneare not supported.
Rules and constraints:
Compared with index assignment, the process of value and operation is increased. The constraint rules of
indexare the same asindexin Index Value, and supportInt,Bool,Tensor,Slice,Ellipse,None,ListandTuple. The values ofIntcontained in the above types of data should be in[-dim_size, dim_size-1]within the closed range.The constraint rules of
valuein the operation process are the same as those ofvaluein index assignment. The type ofvalueneeds to be one of (Number,Tensor,List,Tuple). And ifvalue’s type is notnumber,value.shapeshould be able to broadcast totensor_x[index].shape.For example:
tensor_x = Tensor(np.arange(3 * 4).reshape(3, 4).astype(np.float32)) tensor_y = Tensor(np.arange(3 * 4).reshape(3, 4).astype(np.float32)) tensor_x[[0, 1], 1:3] += 2 tensor_y[[1], ...] -= [4, 3, 2, 1]
The result is as follows:
tensor_x: Tensor(shape=[3, 4], dtype=Float32, value=[[0.0, 3.0, 4.0, 3.0], [4.0, 7.0, 8.0, 7.0], [8.0, 9.0, 10.0, 11.0]]) tensor_y: Tensor(shape=[3, 4], dtype=Float32, value=[[0.0, 1.0, 2.0, 3.0], [0.0, 2.0, 4.0, 6.0], [8.0, 9.0, 10.0, 11.0]])
