SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LMNNImpl.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Fernando J. Iglesias Garcia
8  * Copyright (C) 2013 Fernando J. Iglesias Garcia
9  */
10 
11 #ifndef LMNNIMPL_H_
12 #define LMNNIMPL_H_
13 
14 #include <shogun/lib/config.h>
15 
16 #ifdef HAVE_EIGEN3
17 
18 #include <shogun/lib/common.h>
19 #include <shogun/lib/SGMatrix.h>
23 #include <Eigen/Dense>
24 
25 #include <set>
26 #include <vector>
27 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace shogun
31 {
32 
33 struct CImpostorNode;
34 
35 typedef std::set<CImpostorNode> ImpostorsSetType;
36 
42 struct CImpostorNode
43 {
51  CImpostorNode(index_t ex, index_t tar, index_t imp);
52 
64  bool operator<(const CImpostorNode& rhs) const;
65 
67  index_t example;
68 
70  index_t target;
71 
73  index_t impostor;
74 };
75 
79 class CLMNNImpl
80 {
81  public:
82 
87  static void check_training_setup(CFeatures* features, const CLabels* labels, SGMatrix<float64_t>& init_transform);
88 
93  static SGMatrix<index_t> find_target_nn(CDenseFeatures<float64_t>* x, CMulticlassLabels* y, int32_t k);
94 
96  static Eigen::MatrixXd sum_outer_products(CDenseFeatures<float64_t>* x, const SGMatrix<index_t> target_nn);
97 
99  static ImpostorsSetType find_impostors(CDenseFeatures<float64_t>* x, CMulticlassLabels* y, const Eigen::MatrixXd& L, const SGMatrix<index_t> target_nn, const uint32_t iter, const uint32_t correction);
100 
102  static void update_gradient(CDenseFeatures<float64_t>* x, Eigen::MatrixXd& G, const ImpostorsSetType& Nc, const ImpostorsSetType& Np, float64_t mu);
103 
105  static void gradient_step(Eigen::MatrixXd& L, const Eigen::MatrixXd& G, float64_t stepsize, bool diagonal);
106 
108  static void correct_stepsize(float64_t& stepsize, const SGVector<float64_t> obj, const uint32_t iter);
109 
115  static bool check_termination(float64_t stepsize, const SGVector<float64_t> obj, uint32_t iter, uint32_t maxiter, float64_t stepsize_threshold, float64_t obj_threshold);
116 
117  private:
118 
120  static SGMatrix<float64_t> compute_pca_transform(CDenseFeatures<float64_t>* features);
121 
126  static Eigen::MatrixXd compute_sqdists(Eigen::MatrixXd& L, const SGMatrix<index_t> target_nn);
127 
132  static SGVector<float64_t> compute_impostors_sqdists(Eigen::MatrixXd& L, const ImpostorsSetType& Nexact);
133 
135  static ImpostorsSetType find_impostors_exact(Eigen::MatrixXd& LX, const Eigen::MatrixXd& sqdists, CMulticlassLabels* y, const SGMatrix<index_t> target_nn, int32_t k);
136 
138  static ImpostorsSetType find_impostors_approx(Eigen::MatrixXd& LX, const Eigen::MatrixXd& sqdists, const ImpostorsSetType& Nexact, const SGMatrix<index_t> target_nn);
139 
141  static std::vector<index_t> get_examples_label(CMulticlassLabels* y, float64_t yi);
142 
144  static std::vector<index_t> get_examples_gtlabel(CMulticlassLabels* y, float64_t yi);
145 
151  static CEuclideanDistance* setup_distance(CDenseFeatures<float64_t>* x, std::vector<index_t>& a, std::vector<index_t>& b);
152 
153 
154 }; /* class CLMNNImpl */
155 
156 } /* namespace shogun */
157 
158 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
159 
160 #endif /* HAVE_EIGEN3 */
161 
162 #endif /* _LMNNIMPL_H_ */

SHOGUN Machine Learning Toolbox - Documentation