Sorts values in reverse using `top_k`. Args: values: Tensor of numeric values. axis: Index of the axis which values should be sorted along. return_argsort: If False, return the sorted values. If True, return the indices that would sort the values. Returns: The sorted valu
(values, axis, return_argsort=False)
| 141 | |
| 142 | |
| 143 | def _descending_sort(values, axis, return_argsort=False): |
| 144 | """Sorts values in reverse using `top_k`. |
| 145 | |
| 146 | Args: |
| 147 | values: Tensor of numeric values. |
| 148 | axis: Index of the axis which values should be sorted along. |
| 149 | return_argsort: If False, return the sorted values. If True, return the |
| 150 | indices that would sort the values. |
| 151 | |
| 152 | Returns: |
| 153 | The sorted values. |
| 154 | """ |
| 155 | k = array_ops.shape(values)[axis] |
| 156 | rank = array_ops.rank(values) |
| 157 | static_rank = values.shape.ndims |
| 158 | # Fast path: sorting the last axis. |
| 159 | if axis == -1 or axis + 1 == values.get_shape().ndims: |
| 160 | top_k_input = values |
| 161 | transposition = None |
| 162 | else: |
| 163 | # Otherwise, transpose the array. Swap axes `axis` and `rank - 1`. |
| 164 | if axis < 0: |
| 165 | # Calculate the actual axis index if counting from the end. Use the static |
| 166 | # rank if available, or else make the axis back into a tensor. |
| 167 | axis += static_rank or rank |
| 168 | if static_rank is not None: |
| 169 | # Prefer to calculate the transposition array in NumPy and make it a |
| 170 | # constant. |
| 171 | transposition = constant_op.constant( |
| 172 | np.r_[ |
| 173 | # Axes up to axis are unchanged. |
| 174 | np.arange(axis), |
| 175 | # Swap axis and rank - 1. |
| 176 | [static_rank - 1], |
| 177 | # Axes in [axis + 1, rank - 1) are unchanged. |
| 178 | np.arange(axis + 1, static_rank - 1), |
| 179 | # Swap axis and rank - 1. |
| 180 | [axis]], |
| 181 | name='transposition') |
| 182 | else: |
| 183 | # Generate the transposition array from the tensors. |
| 184 | transposition = array_ops.concat( |
| 185 | [ |
| 186 | # Axes up to axis are unchanged. |
| 187 | math_ops.range(axis), |
| 188 | # Swap axis and rank - 1. |
| 189 | [rank - 1], |
| 190 | # Axes in [axis + 1, rank - 1) are unchanged. |
| 191 | math_ops.range(axis + 1, rank - 1), |
| 192 | # Swap axis and rank - 1. |
| 193 | [axis] |
| 194 | ], |
| 195 | axis=0) |
| 196 | top_k_input = array_ops.transpose(values, transposition) |
| 197 | |
| 198 | values, indices = nn_ops.top_k(top_k_input, k) |
| 199 | return_value = indices if return_argsort else values |
| 200 | if transposition is not None: |