class nbla::TopKData

template<typename T>
class TopKData : public nbla::BaseFunction<int, bool, bool, int, bool, bool>

Select the `k` largest values from each sample in `x` to propagate unmodified and set all other values to 0.

If `abs` is True, the `k` largest values are selected by magnitude. If `reduce` is True (the default), all feature dimensions are reduced to a single dimension of size `k` that propagates only the `k` largest values. Otherwise, if `reduce` is False, input and output dimensions are identical. Dimensions before `base_axis` are treated as number of sample dimensions and `k` values get selected from all elements of a sample (dimensions from `base_axis`) regardless of shape.

Inputs:

  • N-D array

Outputs (1 or 2):

  • N-D array.

  • (Optional) N-D array of top-k indices.

Template Parameters:

T – Data type for computation.

Param k:

Number of largest data values to propagate.

Param abs:

Determine largest data values by magnitude.

Param reduce:

Reduce feature size to one dimension of size `k`.

Param base_axis:

First dimension of the sample shape.

Param largest:

Whether to select the `k` largest or smallest values.

Param with_index:

Return top-k values and indices.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.