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) 2012-2013 Fernando José Iglesias García 00008 * Copyright (C) 2012-2013 Fernando José Iglesias García 00009 */ 00010 00011 #include <shogun/converter/StochasticProximityEmbedding.h> 00012 #include <shogun/lib/config.h> 00013 #ifdef HAVE_EIGEN3 00014 #include <shogun/io/SGIO.h> 00015 #include <shogun/lib/tapkee/tapkee_shogun.hpp> 00016 00017 using namespace shogun; 00018 00019 CStochasticProximityEmbedding::CStochasticProximityEmbedding() : 00020 CEmbeddingConverter() 00021 { 00022 // Initialize to default values 00023 m_k = 12; 00024 m_nupdates = 100; 00025 m_strategy = SPE_GLOBAL; 00026 m_tolerance = 1e-5; 00027 m_max_iteration = 0; 00028 00029 init(); 00030 } 00031 00032 void CStochasticProximityEmbedding::init() 00033 { 00034 SG_ADD(&m_k, "m_k", "Number of neighbors", MS_NOT_AVAILABLE); 00035 SG_ADD((machine_int_t*) &m_strategy, "m_strategy", "SPE strategy", 00036 MS_NOT_AVAILABLE); 00037 SG_ADD(&m_tolerance, "m_tolerance", "Regularization parameter", 00038 MS_NOT_AVAILABLE); 00039 SG_ADD(&m_max_iteration, "max_iteration", "maximum number of iterations", 00040 MS_NOT_AVAILABLE); 00041 } 00042 00043 CStochasticProximityEmbedding::~CStochasticProximityEmbedding() 00044 { 00045 } 00046 00047 void CStochasticProximityEmbedding::set_k(int32_t k) 00048 { 00049 if ( k <= 0 ) 00050 SG_ERROR("Number of neighbors k must be greater than 0") 00051 00052 m_k = k; 00053 } 00054 00055 int32_t CStochasticProximityEmbedding::get_k() const 00056 { 00057 return m_k; 00058 } 00059 00060 void CStochasticProximityEmbedding::set_strategy(ESPEStrategy strategy) 00061 { 00062 m_strategy = strategy; 00063 } 00064 00065 ESPEStrategy CStochasticProximityEmbedding::get_strategy() const 00066 { 00067 return m_strategy; 00068 } 00069 00070 void CStochasticProximityEmbedding::set_tolerance(float32_t tolerance) 00071 { 00072 if ( tolerance <= 0 ) 00073 SG_ERROR("Tolerance regularization parameter must be greater " 00074 "than 0"); 00075 00076 m_tolerance = tolerance; 00077 } 00078 00079 int32_t CStochasticProximityEmbedding::get_tolerance() const 00080 { 00081 return m_tolerance; 00082 } 00083 00084 void CStochasticProximityEmbedding::set_nupdates(int32_t nupdates) 00085 { 00086 if ( nupdates <= 0 ) 00087 SG_ERROR("The number of updates must be greater than 0") 00088 00089 m_nupdates = nupdates; 00090 } 00091 00092 int32_t CStochasticProximityEmbedding::get_nupdates() const 00093 { 00094 return m_nupdates; 00095 } 00096 00097 void CStochasticProximityEmbedding::set_max_iteration(const int32_t max_iteration) 00098 { 00099 m_max_iteration = max_iteration; 00100 } 00101 00102 int32_t CStochasticProximityEmbedding::get_max_iteration() const 00103 { 00104 return m_max_iteration; 00105 } 00106 00107 const char * CStochasticProximityEmbedding::get_name() const 00108 { 00109 return "StochasticProximityEmbedding"; 00110 } 00111 00112 CFeatures* CStochasticProximityEmbedding::apply(CFeatures* features) 00113 { 00114 if ( !features ) 00115 SG_ERROR("Features are required to apply SPE\n") 00116 00117 // Shorthand for the DenseFeatures 00118 CDenseFeatures< float64_t >* simple_features = 00119 (CDenseFeatures< float64_t >*) features; 00120 SG_REF(features); 00121 00122 // Get and check the number of vectors 00123 int32_t N = simple_features->get_num_vectors(); 00124 if ( m_strategy == SPE_LOCAL && m_k >= N ) 00125 SG_ERROR("The number of neighbors (%d) must be less than " 00126 "the number of vectors (%d)\n", m_k, N); 00127 00128 if ( 2*m_nupdates > N ) 00129 SG_ERROR("The number of vectors (%d) must be at least two times " 00130 "the number of updates (%d)\n", N, m_nupdates); 00131 00132 m_distance->init(simple_features, simple_features); 00133 CDenseFeatures< float64_t >* embedding = embed_distance(m_distance); 00134 m_distance->remove_lhs_and_rhs(); 00135 00136 SG_UNREF(features); 00137 return (CFeatures*)embedding; 00138 } 00139 00140 CDenseFeatures< float64_t >* CStochasticProximityEmbedding::embed_distance(CDistance* distance) 00141 { 00142 TAPKEE_PARAMETERS_FOR_SHOGUN parameters; 00143 parameters.n_neighbors = m_k; 00144 parameters.method = SHOGUN_STOCHASTIC_PROXIMITY_EMBEDDING; 00145 parameters.target_dimension = m_target_dim; 00146 parameters.spe_num_updates = m_nupdates; 00147 parameters.spe_tolerance = m_tolerance; 00148 parameters.distance = distance; 00149 parameters.spe_global_strategy = (m_strategy==SPE_GLOBAL); 00150 parameters.max_iteration = m_max_iteration; 00151 CDenseFeatures<float64_t>* embedding = tapkee_embed(parameters); 00152 return embedding; 00153 } 00154 00155 #endif /* HAVE_EIGEN3 */