Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/PolysemousTraining.h
1 /**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 // -*- c++ -*-
9
10 #ifndef FAISS_POLYSEMOUS_TRAINING_INCLUDED
11 #define FAISS_POLYSEMOUS_TRAINING_INCLUDED
12
13
14 #include "ProductQuantizer.h"
15
16
17 namespace faiss {
18
19
20 /// parameters used for the simulated annealing method
21 struct SimulatedAnnealingParameters {
22
23 // optimization parameters
24 double init_temperature; // init probaility of accepting a bad swap
25 double temperature_decay; // at each iteration the temp is multiplied by this
26 int n_iter; // nb of iterations
27 int n_redo; // nb of runs of the simulation
28 int seed; // random seed
29 int verbose;
30 bool only_bit_flips; // restrict permutation changes to bit flips
31 bool init_random; // intialize with a random permutation (not identity)
32
33 // set reasonable defaults
34 SimulatedAnnealingParameters ();
35
36 };
37
38
39 /// abstract class for the loss function
40 struct PermutationObjective {
41
42 int n;
43
44 virtual double compute_cost (const int *perm) const = 0;
45
46 // what would the cost update be if iw and jw were swapped?
47 // default implementation just computes both and computes the difference
48 virtual double cost_update (const int *perm, int iw, int jw) const;
49
50 virtual ~PermutationObjective () {}
51 };
52
53
54 struct ReproduceDistancesObjective : PermutationObjective {
55
56 double dis_weight_factor;
57
58 static double sqr (double x) { return x * x; }
59
60 // weihgting of distances: it is more important to reproduce small
61 // distances well
62 double dis_weight (double x) const;
63
64 std::vector<double> source_dis; ///< "real" corrected distances (size n^2)
65 const double * target_dis; ///< wanted distances (size n^2)
66 std::vector<double> weights; ///< weights for each distance (size n^2)
67
68 double get_source_dis (int i, int j) const;
69
70 // cost = quadratic difference between actual distance and Hamming distance
71 double compute_cost(const int* perm) const override;
72
73 // what would the cost update be if iw and jw were swapped?
74 // computed in O(n) instead of O(n^2) for the full re-computation
75 double cost_update(const int* perm, int iw, int jw) const override;
76
77 ReproduceDistancesObjective (
78 int n,
79 const double *source_dis_in,
80 const double *target_dis_in,
81 double dis_weight_factor);
82
83 static void compute_mean_stdev (const double *tab, size_t n2,
84 double *mean_out, double *stddev_out);
85
86 void set_affine_target_dis (const double *source_dis_in);
87
88 ~ReproduceDistancesObjective() override {}
89 };
90
91 struct RandomGenerator;
92
93 /// Simulated annealing optimization algorithm for permutations.
94 struct SimulatedAnnealingOptimizer: SimulatedAnnealingParameters {
95
96 PermutationObjective *obj;
97 int n; ///< size of the permutation
98 FILE *logfile; /// logs values of the cost function
99
100 SimulatedAnnealingOptimizer (PermutationObjective *obj,
101 const SimulatedAnnealingParameters &p);
102 RandomGenerator *rnd;
103
104 /// remember intial cost of optimization
105 double init_cost;
106
107 // main entry point. Perform the optimization loop, starting from
108 // and modifying permutation in-place
109 double optimize (int *perm);
110
111 // run the optimization and return the best result in best_perm
112 double run_optimization (int * best_perm);
113
114 virtual ~SimulatedAnnealingOptimizer ();
115 };
116
117
118
119
120 /// optimizes the order of indices in a ProductQuantizer
121 struct PolysemousTraining: SimulatedAnnealingParameters {
122
123 enum Optimization_type_t {
124 OT_None,
125 OT_ReproduceDistances_affine, ///< default
126 OT_Ranking_weighted_diff /// same as _2, but use rank of y+ - rank of y-
127 };
128 Optimization_type_t optimization_type;
129
130 // use 1/4 of the training points for the optimization, with
131 // max. ntrain_permutation. If ntrain_permutation == 0: train on
132 // centroids
133 int ntrain_permutation;
134 double dis_weight_factor; // decay of exp that weights distance loss
135
136 // filename pattern for the logging of iterations
137 std::string log_pattern;
138
139 // sets default values
140 PolysemousTraining ();
141
142 /// reorder the centroids so that the Hamming distace becomes a
143 /// good approximation of the SDC distance (called by train)
144 void optimize_pq_for_hamming (ProductQuantizer & pq,
145 size_t n, const float *x) const;
146
147 /// called by optimize_pq_for_hamming
148 void optimize_ranking (ProductQuantizer &pq, size_t n, const float *x) const;
149 /// called by optimize_pq_for_hamming
150 void optimize_reproduce_distances (ProductQuantizer &pq) const;
151
152 };
153
154
155 } // namespace faiss
156
157
158 #endif
faiss::RandomGenerator
random generator that can be used in multithreaded contexts
Definition: utils.h:47
faiss::PolysemousTraining::OT_Ranking_weighted_diff
same as _2, but use rank of y+ - rank of y-
Definition: PolysemousTraining.h:126
faiss::SimulatedAnnealingOptimizer::SimulatedAnnealingOptimizer
SimulatedAnnealingOptimizer(PermutationObjective *obj, const SimulatedAnnealingParameters &p)
logs values of the cost function
Definition: PolysemousTraining.cpp:68
faiss::ReproduceDistancesObjective
Definition: PolysemousTraining.h:54
faiss::SimulatedAnnealingOptimizer::n
int n
size of the permutation
Definition: PolysemousTraining.h:97
faiss::ReproduceDistancesObjective::target_dis
const double * target_dis
wanted distances (size n^2)
Definition: PolysemousTraining.h:65
faiss::SimulatedAnnealingOptimizer::init_cost
double init_cost
remember intial cost of optimization
Definition: PolysemousTraining.h:105
faiss::PolysemousTraining::optimize_ranking
void optimize_ranking(ProductQuantizer &pq, size_t n, const float *x) const
called by optimize_pq_for_hamming
Definition: PolysemousTraining.cpp:827
faiss::PolysemousTraining
optimizes the order of indices in a ProductQuantizer
Definition: PolysemousTraining.h:121
faiss::ReproduceDistancesObjective::weights
std::vector< double > weights
weights for each distance (size n^2)
Definition: PolysemousTraining.h:66
faiss::SimulatedAnnealingParameters
parameters used for the simulated annealing method
Definition: PolysemousTraining.h:21
faiss::PolysemousTraining::OT_ReproduceDistances_affine
default
Definition: PolysemousTraining.h:125
faiss::PermutationObjective
abstract class for the loss function
Definition: PolysemousTraining.h:40
faiss::PolysemousTraining::Optimization_type_t
Optimization_type_t
Definition: PolysemousTraining.h:123
faiss::ProductQuantizer
Definition: ProductQuantizer.h:23
faiss::ReproduceDistancesObjective::source_dis
std::vector< double > source_dis
&quot;real&quot; corrected distances (size n^2)
Definition: PolysemousTraining.h:64
faiss::PolysemousTraining::optimize_reproduce_distances
void optimize_reproduce_distances(ProductQuantizer &pq) const
called by optimize_pq_for_hamming
Definition: PolysemousTraining.cpp:764
faiss::PolysemousTraining::optimize_pq_for_hamming
void optimize_pq_for_hamming(ProductQuantizer &pq, size_t n, const float *x) const
Definition: PolysemousTraining.cpp:934
faiss::SimulatedAnnealingOptimizer
Simulated annealing optimization algorithm for permutations.
Definition: PolysemousTraining.h:94