tensorflow/examples/speech_commands/label_wav.cc
// Analyzes the output of the graph to retrieve the highest scores and
// their positions in the tensor.
Analyzes the output of the graph to retrieve the highest scores and
their positions in the tensor.
Analyzes the output of the graph to retrieve the highest scores and their positions in the tensor .
void GetTopLabels(const std::vector<Tensor>& outputs, int how_many_labels,
                  Tensor* out_indices, Tensor* out_scores) {
  const Tensor& unsorted_scores_tensor = outputs[0];
  auto unsorted_scores_flat = unsorted_scores_tensor.flat<float>();
  std::vector<std::pair<int, float>> scores;
  scores.reserve(unsorted_scores_flat.size());
  for (int i = 0; i < unsorted_scores_flat.size(); ++i) {
    scores.push_back(std::pair<int, float>({i, unsorted_scores_flat(i)}));
  }
  std::sort(scores.begin(), scores.end(),
            [](const std::pair<int, float>& left,
               const std::pair<int, float>& right) {
              return left.second > right.second;
            });
  scores.resize(how_many_labels);
  Tensor sorted_indices(tensorflow::DT_INT32, {how_many_labels});
  Tensor sorted_scores(tensorflow::DT_FLOAT, {how_many_labels});
  for (int i = 0; i < scores.size(); ++i) {
    sorted_indices.flat<int>()(i) = scores[i].first;
    sorted_scores.flat<float>()(i) = scores[i].second;
  }
  *out_indices = sorted_indices;
  *out_scores = sorted_scores;
}
void GetTopLabels ( const std :: vector < Tensor > & outputs , int how_many_labels , Tensor * out_indices , Tensor * out_scores ) { const Tensor & unsorted_scores_tensor = outputs [ 0 ] ; auto unsorted_scores_flat = unsorted_scores_tensor . flat < float > ( ) ; std :: vector < std :: pair < int , float > > scores ; scores . reserve ( unsorted_scores_flat . size ( ) ) ; for ( int i = 0 ; i < unsorted_scores_flat . size ( ) ; ++ i ) { scores . push_back ( std :: pair < int , float > ( { i , unsorted_scores_flat ( i ) } ) ) ; } std :: sort ( scores . begin ( ) , scores . end ( ) , [ ] ( const std :: pair < int , float > & left , const std :: pair < int , float > & right ) { return left . second > right . second ; } ) ; scores . resize ( how_many_labels ) ; Tensor sorted_indices ( tensorflow :: DT_INT32 , { how_many_labels } ) ; Tensor sorted_scores ( tensorflow :: DT_FLOAT , { how_many_labels } ) ; for ( int i = 0 ; i < scores . size ( ) ; ++ i ) { sorted_indices . flat < int > ( ) ( i ) = scores [ i ] . first ; sorted_scores . flat < float > ( ) ( i ) = scores [ i ] . second ; } * out_indices = sorted_indices ; * out_scores = sorted_scores ; }
Analyzes the output of the graph to retrieve the highest scores and
their positions in the tensor.
Analyzes the output of the graph to retrieve the highest scores and their positions in the tensor .