Welcome to our Support Center

TopK

Description

Retrieve the top-K largest or smallest elements along a specified axis.

 

 

Given an input tensor of shape [a_0, a_1, …, a_{n-1}] and integer argument k, return two outputs:

  • Value tensor of shape [a_0, a_1, …, a_{axis-1}, k, a_{axis+1}, … a_{n-1}] which contains the values of the top k elements along the specified axis
  • Index tensor of shape [a_0, a_1, …, a_{axis-1}, k, a_{axis+1}, … a_{n-1}] which contains the indices of the top k elements (original indices from the input tensor).
  • If “largest” is 1 (the default value) then the k largest elements are returned.
  • If “sorted” is 1 (the default value) then the resulting k elements will be sorted.
  • If “sorted” is 0, order of returned ‘Values’ and ‘Indices’ are undefined.

 

Given two equivalent values, this operator uses the indices along the axis as a tiebreaker. That is, the element with the lower index will appear first.

 

Input parameters

 

specified_outputs_namearray, this parameter lets you manually assign custom names to the output tensors of a node.

 Graphs in : cluster, ONNX model architecture.

X (heterogeneous) – T : object, tensor of shape [a_0, a_1, …, a_{n-1}].
K (heterogeneous) – tensor(int64) : object, a 1-D tensor containing a single positive value corresponding to the number of top elements to retrieve.

 Parameters : cluster,

axis : integer, dimension on which to do the sort. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input).
Default value “-1”.
largest : boolean, whether to return the top-K largest or smallest elements.
Default value “True”.
sorted : boolean, whether to return the elements in sorted order.
Default value “True”.
 training? : boolean, whether the layer is in training mode (can store data for backward).
Default value “True”.
 lda coeff : float, defines the coefficient by which the loss derivative will be multiplied before being sent to the previous layer (since during the backward run we go backwards).
Default value “1”.

 name (optional) : string, name of the node.

Output parameters

 Graphs out : cluster, ONNX model architecture.

Values (heterogeneous) – T : object, tensor of shape [a_0, a_1, …, a_{axis-1}, k, a_{axis+1}, … a_{n-1}] containing top K values from the input tensor.
Indices (heterogeneous) – I : object, tensor of shape [a_0, a_1, …, a_{axis-1}, k, a_{axis+1}, … a_{n-1}] containing the corresponding input tensor indices for the top K values.

Type Constraints

T in (tensor(double)tensor(float)tensor(float16)tensor(int16)tensor(int32)tensor(int64)tensor(int8)
tensor(uint16)tensor(uint32)tensor(uint64)tensor(uint8)) : Constrain input and output types to numeric tensors.

I in (tensor(int64)) : Constrain index tensor to int64.

Example

All these exemples are snippets PNG, you can drop these Snippet onto the block diagram and get the depicted code added to your VI (Do not forget to install Deep Learning library to run it).
Table of Contents