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 Roman Votyakov 00008 * Copyright (C) 2012 Jacob Walker 00009 */ 00010 00011 #ifndef CGRADIENTRESULT_H_ 00012 #define CGRADIENTRESULT_H_ 00013 00014 #include <shogun/evaluation/EvaluationResult.h> 00015 #include <shogun/lib/Map.h> 00016 00017 namespace shogun 00018 { 00019 00023 class CGradientResult : public CEvaluationResult 00024 { 00025 public: 00027 CGradientResult() : CEvaluationResult() 00028 { 00029 m_total_variables=0; 00030 m_gradient=NULL; 00031 m_parameter_dictionary=NULL; 00032 } 00033 00034 virtual ~CGradientResult() 00035 { 00036 SG_UNREF(m_gradient); 00037 SG_UNREF(m_parameter_dictionary); 00038 } 00039 00044 virtual const char* get_name() const { return "GradientResult"; } 00045 00052 static CGradientResult* obtain_from_generic(CEvaluationResult* eval_result) 00053 { 00054 ASSERT(eval_result); 00055 00056 REQUIRE(eval_result->get_result_type()==GRADIENTEVALUATION_RESULT, 00057 "Provided evaluation result is not of type CGradientResult!\n") 00058 00059 SG_REF(eval_result); 00060 return (CGradientResult*) eval_result; 00061 } 00062 00067 virtual EEvaluationResultType get_result_type() const 00068 { 00069 return GRADIENTEVALUATION_RESULT; 00070 } 00071 00073 virtual void print_result() 00074 { 00075 REQUIRE(m_gradient, "Gradient map should not be NULL\n") 00076 REQUIRE(m_parameter_dictionary, "Parameter dictionary should not be " 00077 "NULL\n") 00078 00079 // print value of the function 00080 SG_SPRINT("Value: [") 00081 00082 for (index_t i=0; i<m_value.vlen-1; i++) 00083 SG_SPRINT("%f, ", m_value[i]) 00084 00085 if (m_value.vlen>0) 00086 SG_SPRINT("%f", m_value[m_value.vlen-1]) 00087 00088 SG_SPRINT("] ") 00089 00090 // print gradient wrt parameters 00091 SG_SPRINT("Gradient: [") 00092 00093 for (index_t i=0; i<m_gradient->get_num_elements(); i++) 00094 { 00095 CMapNode<TParameter*, SGVector<float64_t> >* param_node= 00096 m_gradient->get_node_ptr(i); 00097 00098 // get parameter name 00099 const char* param_name=param_node->key->m_name; 00100 00101 // get object name 00102 const char* object_name= 00103 m_parameter_dictionary->get_element(param_node->key)->get_name(); 00104 00105 // get gradient wrt parameter 00106 SGVector<float64_t> param_gradient=param_node->data; 00107 00108 SG_PRINT("%s.%s: ", object_name, param_name) 00109 00110 for (index_t j=0; j<param_gradient.vlen-1; j++) 00111 SG_SPRINT("%f, ", param_gradient[j]) 00112 00113 if (i==m_gradient->get_num_elements()-1) 00114 { 00115 if (param_gradient.vlen>0) 00116 SG_PRINT("%f", param_gradient[param_gradient.vlen-1]) 00117 } 00118 else 00119 { 00120 if (param_gradient.vlen>0) 00121 SG_PRINT("%f; ", param_gradient[param_gradient.vlen-1]) 00122 } 00123 } 00124 00125 SG_SPRINT("] Total Variables: %u\n", m_total_variables) 00126 } 00127 00132 virtual uint32_t get_total_variables() 00133 { 00134 return m_total_variables; 00135 } 00136 00141 virtual void set_value(SGVector<float64_t> value) 00142 { 00143 m_value=SGVector<float64_t>(value); 00144 } 00145 00150 virtual SGVector<float64_t> get_value() 00151 { 00152 return SGVector<float64_t>(m_value); 00153 } 00154 00159 virtual void set_gradient(CMap<TParameter*, SGVector<float64_t> >* gradient) 00160 { 00161 REQUIRE(gradient, "Gradient map should not be NULL\n") 00162 00163 SG_REF(gradient); 00164 SG_UNREF(m_gradient); 00165 m_gradient=gradient; 00166 00167 m_total_variables=0; 00168 00169 for (index_t i=0; i<gradient->get_num_elements(); i++) 00170 { 00171 CMapNode<TParameter*, SGVector<float64_t> >* node= 00172 m_gradient->get_node_ptr(i); 00173 m_total_variables+=node->data.vlen; 00174 } 00175 } 00176 00181 virtual CMap<TParameter*, SGVector<float64_t> >* get_gradient() 00182 { 00183 SG_REF(m_gradient); 00184 return m_gradient; 00185 } 00186 00191 virtual void set_paramter_dictionary( 00192 CMap<TParameter*, CSGObject*>* parameter_dictionary) 00193 { 00194 SG_REF(parameter_dictionary); 00195 SG_UNREF(m_parameter_dictionary); 00196 m_parameter_dictionary=parameter_dictionary; 00197 } 00198 00203 virtual CMap<TParameter*, CSGObject*>* get_paramter_dictionary() 00204 { 00205 SG_REF(m_parameter_dictionary); 00206 return m_parameter_dictionary; 00207 } 00208 00209 private: 00211 SGVector<float64_t> m_value; 00212 00214 CMap<TParameter*, SGVector<float64_t> >* m_gradient; 00215 00217 CMap<TParameter*, CSGObject*>* m_parameter_dictionary; 00218 00220 uint32_t m_total_variables; 00221 }; 00222 } 00223 #endif /* CGRADIENTRESULT_H_ */