SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2013 Fernando J. Iglesias Garcia 00008 * Copyright (C) 2013 Fernando J. Iglesias Garcia 00009 */ 00010 00011 #ifndef LMNNIMPL_H_ 00012 #define LMNNIMPL_H_ 00013 00014 #include <shogun/lib/config.h> 00015 00016 #ifdef HAVE_EIGEN3 00017 #ifdef HAVE_LAPACK 00018 00019 #include <shogun/lib/common.h> 00020 #include <shogun/lib/SGMatrix.h> 00021 #include <shogun/features/DenseFeatures.h> 00022 #include <shogun/labels/MulticlassLabels.h> 00023 #include <shogun/distance/EuclideanDistance.h> 00024 #include <Eigen/Dense> 00025 00026 #include <set> 00027 #include <vector> 00028 00029 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00030 00031 namespace shogun 00032 { 00033 00034 struct CImpostorNode; 00035 00036 typedef std::set<CImpostorNode> ImpostorsSetType; 00037 00043 struct CImpostorNode 00044 { 00052 CImpostorNode(index_t ex, index_t tar, index_t imp); 00053 00065 bool operator<(const CImpostorNode& rhs) const; 00066 00068 index_t example; 00069 00071 index_t target; 00072 00074 index_t impostor; 00075 }; 00076 00080 class CLMNNImpl 00081 { 00082 public: 00083 00088 static void check_training_setup(CFeatures* features, const CLabels* labels, SGMatrix<float64_t>& init_transform); 00089 00094 static SGMatrix<index_t> find_target_nn(CDenseFeatures<float64_t>* x, CMulticlassLabels* y, int32_t k); 00095 00097 static Eigen::MatrixXd sum_outer_products(CDenseFeatures<float64_t>* x, const SGMatrix<index_t> target_nn); 00098 00100 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); 00101 00103 static void update_gradient(CDenseFeatures<float64_t>* x, Eigen::MatrixXd& G, const ImpostorsSetType& Nc, const ImpostorsSetType& Np, float64_t mu); 00104 00106 static void gradient_step(Eigen::MatrixXd& L, const Eigen::MatrixXd& G, float64_t stepsize, bool diagonal); 00107 00109 static void correct_stepsize(float64_t& stepsize, const SGVector<float64_t> obj, const uint32_t iter); 00110 00116 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); 00117 00118 private: 00119 00121 static SGMatrix<float64_t> compute_pca_transform(CDenseFeatures<float64_t>* features); 00122 00127 static Eigen::MatrixXd compute_sqdists(Eigen::MatrixXd& L, const SGMatrix<index_t> target_nn); 00128 00133 static SGVector<float64_t> compute_impostors_sqdists(Eigen::MatrixXd& L, const ImpostorsSetType& Nexact); 00134 00136 static ImpostorsSetType find_impostors_exact(Eigen::MatrixXd& LX, const Eigen::MatrixXd& sqdists, CMulticlassLabels* y, const SGMatrix<index_t> target_nn, int32_t k); 00137 00139 static ImpostorsSetType find_impostors_approx(Eigen::MatrixXd& LX, const Eigen::MatrixXd& sqdists, const ImpostorsSetType& Nexact, const SGMatrix<index_t> target_nn); 00140 00142 static std::vector<index_t> get_examples_label(CMulticlassLabels* y, float64_t yi); 00143 00145 static std::vector<index_t> get_examples_gtlabel(CMulticlassLabels* y, float64_t yi); 00146 00152 static CEuclideanDistance* setup_distance(CDenseFeatures<float64_t>* x, std::vector<index_t>& a, std::vector<index_t>& b); 00153 00154 00155 }; /* class CLMNNImpl */ 00156 00157 } /* namespace shogun */ 00158 00159 #endif /* DOXYGEN_SHOULD_SKIP_THIS */ 00160 00161 #endif /* HAVE_LAPACK */ 00162 #endif /* HAVE_EIGEN3 */ 00163 00164 #endif /* _LMNNIMPL_H_ */