public String decodePredictions ( INDArray predictions ) { Preconditions . checkState ( predictions . size ( 1 ) == predictionLabels . size ( ) , "Invalid input array:" + " expected array with size(1) equal to numLabels (%s), got array with shape %s" , predictionLabels . size ( ) , predictions . shape ( ) ) ; String predictionDescription = "" ; int [ ] top5 = new int [ 5 ] ; float [ ] top5Prob = new float [ 5 ] ; //brute force collect top 5 int i = 0 ; for ( int batch = 0 ; batch < predictions . size ( 0 ) ; batch ++ ) { predictionDescription += "Predictions for batch " ; if ( predictions . size ( 0 ) > 1 ) { predictionDescription += String . valueOf ( batch ) ; } predictionDescription += " :" ; INDArray currentBatch = predictions . getRow ( batch ) . dup ( ) ; while ( i < 5 ) { top5 [ i ] = Nd4j . argMax ( currentBatch , 1 ) . getInt ( 0 ) ; top5Prob [ i ] = currentBatch . getFloat ( batch , top5 [ i ] ) ; currentBatch . putScalar ( 0 , top5 [ i ] , 0 ) ; predictionDescription += "\n\t" + String . format ( "%3f" , top5Prob [ i ] * 100 ) + "%, " + predictionLabels . get ( top5 [ i ] ) ; i ++ ; } } return predictionDescription ; }
Given predictions from the trained model this method will return a string listing the top five matches and the respective probabilities