Chunk and combine function of topk Extract the k largest elements from a on the given axis. If k is negative, extract the -k smallest elements instead. Note that, unlike in the parent function, the returned elements are not sorted internally.
(a, k, axis, keepdims)
| 171 | |
| 172 | |
| 173 | def topk(a, k, axis, keepdims): |
| 174 | """Chunk and combine function of topk |
| 175 | |
| 176 | Extract the k largest elements from a on the given axis. |
| 177 | If k is negative, extract the -k smallest elements instead. |
| 178 | Note that, unlike in the parent function, the returned elements |
| 179 | are not sorted internally. |
| 180 | """ |
| 181 | assert keepdims is True |
| 182 | axis = axis[0] |
| 183 | if abs(k) >= a.shape[axis]: |
| 184 | return a |
| 185 | |
| 186 | a = np.partition(a, -k, axis=axis) |
| 187 | k_slice = slice(-k, None) if k > 0 else slice(-k) |
| 188 | return a[tuple(k_slice if i == axis else slice(None) for i in range(a.ndim))] |
| 189 | |
| 190 | |
| 191 | def topk_aggregate(a, k, axis, keepdims): |