index_sample¶
- paddle. index_sample ( x, index ) [source]
-
IndexSample Layer
IndexSample OP returns the element of the specified location of X, and the location is specified by Index.
Given: X = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] Index = [[0, 1, 3], [0, 2, 4]] Then: Out = [[1, 2, 4], [6, 8, 10]]
- Parameters
-
x (Tensor) – The source input tensor with 2-D shape. Supported data type is int32, int64, bfloat16, float16, float32, float64, complex64, complex128.
index (Tensor) – The index input tensor with 2-D shape, first dimension should be same with X. Data type is int32 or int64.
- Returns
-
The output is a tensor with the same shape as index.
- Return type
-
output (Tensor)
Examples
>>> import paddle >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], ... [5.0, 6.0, 7.0, 8.0], ... [9.0, 10.0, 11.0, 12.0]], dtype='float32') >>> index = paddle.to_tensor([[0, 1, 2], ... [1, 2, 3], ... [0, 0, 0]], dtype='int32') >>> target = paddle.to_tensor([[100, 200, 300, 400], ... [500, 600, 700, 800], ... [900, 1000, 1100, 1200]], dtype='int32') >>> out_z1 = paddle.index_sample(x, index) >>> print(out_z1.numpy()) [[1. 2. 3.] [6. 7. 8.] [9. 9. 9.]] >>> # Use the index of the maximum value by topk op >>> # get the value of the element of the corresponding index in other tensors >>> top_value, top_index = paddle.topk(x, k=2) >>> out_z2 = paddle.index_sample(target, top_index) >>> print(top_value.numpy()) [[ 4. 3.] [ 8. 7.] [12. 11.]] >>> print(top_index.numpy()) [[3 2] [3 2] [3 2]] >>> print(out_z2.numpy()) [[ 400 300] [ 800 700] [1200 1100]]