|
212 | 212 | 'flip',
|
213 | 213 | 'roll',
|
214 | 214 | 'log_softmax',
|
| 215 | + 'index_sample', |
215 | 216 | ]
|
216 | 217 |
|
217 | 218 |
|
@@ -16555,3 +16556,84 @@ def log_softmax(input, axis=None, dtype=None, name=None):
|
16555 | 16556 | type='log', inputs={'X': outs_softmax}, outputs={'Out': outs_log})
|
16556 | 16557 |
|
16557 | 16558 | return outs_log
|
| 16559 | + |
| 16560 | + |
| 16561 | +def index_sample(x, index): |
| 16562 | + """ |
| 16563 | + **IndexSample Layer** |
| 16564 | + IndexSample OP returns the element of the specified location of X, |
| 16565 | + and the location is specified by Index. |
| 16566 | + |
| 16567 | + .. code-block:: text |
| 16568 | + |
| 16569 | + Args: |
| 16570 | + x (Variable): The source input tensor with 2-D shape. Supported data type is |
| 16571 | + int32, int64, float32, float64. |
| 16572 | + index (Variable): The index input tensor with 2-D shape, first dimension should be same with X. |
| 16573 | + Data type is int32 or int64. |
| 16574 | + |
| 16575 | + Returns: |
| 16576 | + Variable: A tensor with the same shape as `index` . |
| 16577 | + |
| 16578 | + Examples: |
| 16579 | + .. code-block:: python |
| 16580 | + |
| 16581 | + import paddle.fluid as fluid |
| 16582 | + import numpy as np |
| 16583 | + |
| 16584 | + data = np.array([[1.0, 2.0, 3.0, 4.0], |
| 16585 | + [5.0, 6.0, 7.0, 8.0], |
| 16586 | + [9.0, 10.0, 11.0, 12.0]]).astype('float32') |
| 16587 | + |
| 16588 | + data_index = np.array([[0, 1, 2], |
| 16589 | + [1, 2, 3], |
| 16590 | + [0, 0, 0]]).astype('int32') |
| 16591 | + |
| 16592 | + target_data = np.array([[100, 200, 300, 400], |
| 16593 | + [500, 600, 700, 800], |
| 16594 | + [900, 1000, 1100, 1200]]).astype('int32') |
| 16595 | + |
| 16596 | + with fluid.dygraph.guard(): |
| 16597 | + x = fluid.dygraph.to_variable(data) |
| 16598 | + index = fluid.dygraph.to_variable(data_index) |
| 16599 | + target = fluid.dygraph.to_variable(target_data) |
| 16600 | + |
| 16601 | + out_z1 = fluid.layers.index_sample(x, index) |
| 16602 | + print(out_z1.numpy()) |
| 16603 | + #[[1. 2. 3.] |
| 16604 | + # [6. 7. 8.] |
| 16605 | + # [9. 9. 9.]] |
| 16606 | + |
| 16607 | + # Use the index of the maximum value by topk op |
| 16608 | + # get the value of the element of the corresponding index in other tensors |
| 16609 | + top_value, top_index = fluid.layers.topk(x, k=2) |
| 16610 | + out_z2 = fluid.layers.index_sample(target, top_index) |
| 16611 | + print(top_value.numpy()) |
| 16612 | + #[[ 4. 3.] |
| 16613 | + # [ 8. 7.] |
| 16614 | + # [12. 11.]] |
| 16615 | + |
| 16616 | + print(top_index.numpy()) |
| 16617 | + #[[3 2] |
| 16618 | + # [3 2] |
| 16619 | + # [3 2]] |
| 16620 | + |
| 16621 | + print(out_z2.numpy()) |
| 16622 | + #[[ 400 300] |
| 16623 | + # [ 800 700] |
| 16624 | + # [1200 1100]] |
| 16625 | + """ |
| 16626 | + helper = LayerHelper("index_sample", **locals()) |
| 16627 | + |
| 16628 | + check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'], |
| 16629 | + 'fluid.layers.index_sample') |
| 16630 | + check_variable_and_dtype(index, 'index', ['int32', 'int64'], |
| 16631 | + 'fluid.layers.index_sample') |
| 16632 | + out = helper.create_variable_for_type_inference(dtype=x.dtype) |
| 16633 | + |
| 16634 | + helper.append_op( |
| 16635 | + type='index_sample', |
| 16636 | + inputs={'X': x, |
| 16637 | + 'Index': index}, |
| 16638 | + outputs={'Out': out}) |
| 16639 | + return out |
0 commit comments