license import * as tf from '@tensorflow/tfjs-core' ; export function concatWithNulls ( ndarray1 , ndarray2 ) { if ( ndarray1 == null && ndarray2 == null ) { return null ; } if ( ndarray1 == null ) { return ndarray2 . clone ( ) ; } else if ( ndarray2 === null ) { return ndarray1 . clone ( ) ; } return tf . concat < tf . Tensor2D > ( [ ndarray1 , ndarray2 ] , 0 ) ; } export function topK ( values , k ) { const valuesAndIndices = [ ] ; for ( let i = 0 ; i < values . length ; i ++ ) { valuesAndIndices . push ( { value : values [ i ] , index : i } ) ; } valuesAndIndices . sort ( ( a , b ) => { return b . value - a . value ; } ) ; const topkValues = new Float32Array ( k ) ; const topkIndices = new Int32Array ( k ) ; for ( let i = 0 ; i < k ; i ++ ) { topkValues [ i ] = valuesAndIndices [ i ] . value ; topkIndices [ i ] = valuesAndIndices [ i ] . index ; } return { values : topkValues , indices : topkIndices } ; }