SOMHunter Core
embedding-ranker.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef EMBEDDING_RANKER_H_
22 #define EMBEDDING_RANKER_H_
23 
24 #include <cassert>
25 #include <execution>
26 #include <fstream>
27 #include <iomanip>
28 #include <map>
29 #include <set>
30 #include <string>
31 #include <vector>
32 
33 #include "common.h"
34 
35 #include "dataset-frames.h"
36 #include "distances.hpp"
37 #include "scores.h"
38 #include "settings.h"
39 #include "utils.hpp"
40 
41 namespace sh {
42 template <typename SpecificFrameFeatures>
44 public:
45  virtual ~EmbeddingRanker() noexcept {}
46 
47 protected:
48  std::vector<float> inverse_score_vector(const std::vector<float>& query_vecs,
49  const SpecificFrameFeatures& _features) const;
50 
51  std::vector<float> inverse_score_vector(const float* query_vecs, const SpecificFrameFeatures& _features) const;
52 };
53 
54 template <typename SpecificFrameFeatures>
56  const std::vector<float>& query_vec, const SpecificFrameFeatures& _dataset_features) const {
57  return inverse_score_vector(query_vec.data(), _dataset_features);
58 }
59 
60 template <typename SpecificFrameFeatures>
62  const float* query_vec, const SpecificFrameFeatures& features) const {
63  size_t target_dim{ features.dim() };
64 
65  // Result is final score \in [0.0F, 1.0F] of `query_vec` as temporal query
66  std::vector<float> scores;
67  scores.resize(features.size());
68 
69  // For all frames, comute the distance
70  std::for_each(std::execution::par_unseq, ioterable<FrameId>(0), ioterable<FrameId>(features.size()),
71  [&, this](FrameId frame_ID) {
72  const float* raw_frame_features = features.fv(frame_ID);
73 
74  // auto dist = utils::d_cos_normalized(query_vec, raw_frame_features, target_dim) / 2.0f;
75  auto dist = d_cos_normalized(query_vec, raw_frame_features, target_dim) / 2.0F;
76 
77  scores[frame_ID] = dist;
78  });
79  // Serial version
80  // for (FrameId frame_ID = 0; frame_ID < features.size(); ++frame_ID) {
81  // const float* raw_frame_features = features.fv(frame_ID);
82 
83  // // auto dist = utils::d_cos_normalized(query_vec, raw_frame_features, target_dim) / 2.0f;
84  // auto dist = d_cos_normalized(query_vec, raw_frame_features, target_dim) / 2.0F;
85 
86  // scores[frame_ID] = dist;
87  //}
88 
89  return scores;
90 }
91 
92 } // namespace sh
93 
94 #endif // EMBEDDING_RANKER_H_
Definition: embedding-ranker.h:43
std::vector< float > inverse_score_vector(const std::vector< float > &query_vecs, const SpecificFrameFeatures &_features) const
Definition: embedding-ranker.h:55
std::vector< float > inverse_score_vector(const float *query_vecs, const SpecificFrameFeatures &_features) const
Definition: embedding-ranker.h:61
virtual ~EmbeddingRanker() noexcept
Definition: embedding-ranker.h:45
File implementing distance calculations on vectors.
Definition: common-types.h:33
unsigned long FrameId
Definition: common-types.h:75
Until C++20 and ranges come.
Definition: common.h:65