SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GradientResult.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 Roman Votyakov
8  * Copyright (C) 2012 Jacob Walker
9  */
10 
11 #ifndef CGRADIENTRESULT_H_
12 #define CGRADIENTRESULT_H_
13 
14 #include <shogun/lib/config.h>
15 
17 #include <shogun/lib/Map.h>
18 
19 namespace shogun
20 {
21 
26 {
27 public:
30  {
31  m_total_variables=0;
32  m_gradient=NULL;
33  m_parameter_dictionary=NULL;
34  }
35 
36  virtual ~CGradientResult()
37  {
38  SG_UNREF(m_gradient);
39  SG_UNREF(m_parameter_dictionary);
40  }
41 
46  virtual const char* get_name() const { return "GradientResult"; }
47 
55  {
56  ASSERT(eval_result);
57 
59  "Provided evaluation result is not of type CGradientResult!\n")
60 
61  SG_REF(eval_result);
62  return (CGradientResult*) eval_result;
63  }
64 
70  {
72  }
73 
75  virtual void print_result()
76  {
77  REQUIRE(m_gradient, "Gradient map should not be NULL\n")
78  REQUIRE(m_parameter_dictionary, "Parameter dictionary should not be "
79  "NULL\n")
80 
81  // print value of the function
82  SG_SPRINT("Value: [")
83 
84  for (index_t i=0; i<m_value.vlen-1; i++)
85  SG_SPRINT("%f, ", m_value[i])
86 
87  if (m_value.vlen>0)
88  SG_SPRINT("%f", m_value[m_value.vlen-1])
89 
90  SG_SPRINT("] ")
91 
92  // print gradient wrt parameters
93  SG_SPRINT("Gradient: [")
94 
95  for (index_t i=0; i<m_gradient->get_num_elements(); i++)
96  {
97  CMapNode<TParameter*, SGVector<float64_t> >* param_node=
98  m_gradient->get_node_ptr(i);
99 
100  // get parameter name
101  const char* param_name=param_node->key->m_name;
102 
103  // get object name
104  const char* object_name=
105  m_parameter_dictionary->get_element(param_node->key)->get_name();
106 
107  // get gradient wrt parameter
108  SGVector<float64_t> param_gradient=param_node->data;
109 
110  SG_PRINT("%s.%s: ", object_name, param_name)
111 
112  for (index_t j=0; j<param_gradient.vlen-1; j++)
113  SG_SPRINT("%f, ", param_gradient[j])
114 
115  if (i==m_gradient->get_num_elements()-1)
116  {
117  if (param_gradient.vlen>0)
118  SG_PRINT("%f", param_gradient[param_gradient.vlen-1])
119  }
120  else
121  {
122  if (param_gradient.vlen>0)
123  SG_PRINT("%f; ", param_gradient[param_gradient.vlen-1])
124  }
125  }
126 
127  SG_SPRINT("] Total Variables: %u\n", m_total_variables)
128  }
129 
134  virtual uint32_t get_total_variables()
135  {
136  return m_total_variables;
137  }
138 
143  virtual void set_value(SGVector<float64_t> value)
144  {
145  m_value=SGVector<float64_t>(value);
146  }
147 
153  {
154  return SGVector<float64_t>(m_value);
155  }
156 
161  virtual void set_gradient(CMap<TParameter*, SGVector<float64_t> >* gradient)
162  {
163  REQUIRE(gradient, "Gradient map should not be NULL\n")
164 
165  SG_REF(gradient);
166  SG_UNREF(m_gradient);
167  m_gradient=gradient;
168 
169  m_total_variables=0;
170 
171  for (index_t i=0; i<gradient->get_num_elements(); i++)
172  {
173  CMapNode<TParameter*, SGVector<float64_t> >* node=
174  m_gradient->get_node_ptr(i);
175  m_total_variables+=node->data.vlen;
176  }
177  }
178 
184  {
185  SG_REF(m_gradient);
186  return m_gradient;
187  }
188 
194  CMap<TParameter*, CSGObject*>* parameter_dictionary)
195  {
196  SG_REF(parameter_dictionary);
197  SG_UNREF(m_parameter_dictionary);
198  m_parameter_dictionary=parameter_dictionary;
199  }
200 
206  {
207  SG_REF(m_parameter_dictionary);
208  return m_parameter_dictionary;
209  }
210 
211 private:
213  SGVector<float64_t> m_value;
214 
217 
219  CMap<TParameter*, CSGObject*>* m_parameter_dictionary;
220 
222  uint32_t m_total_variables;
223 };
224 }
225 #endif /* CGRADIENTRESULT_H_ */
int32_t index_t
Definition: common.h:62
virtual void set_value(SGVector< float64_t > value)
parameter struct
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual EEvaluationResultType get_result_type() const
#define SG_REF(x)
Definition: SGObject.h:51
virtual void set_gradient(CMap< TParameter *, SGVector< float64_t > > *gradient)
virtual void print_result()
virtual void set_paramter_dictionary(CMap< TParameter *, CSGObject * > *parameter_dictionary)
static CGradientResult * obtain_from_generic(CEvaluationResult *eval_result)
index_t vlen
Definition: SGVector.h:494
virtual uint32_t get_total_variables()
#define SG_PRINT(...)
Definition: SGIO.h:137
#define SG_SPRINT(...)
Definition: SGIO.h:180
#define ASSERT(x)
Definition: SGIO.h:201
virtual CMap< TParameter *, CSGObject * > * get_paramter_dictionary()
Abstract class that contains the result generated by the MachineEvaluation class. ...
virtual CMap< TParameter *, SGVector< float64_t > > * get_gradient()
#define SG_UNREF(x)
Definition: SGObject.h:52
virtual const char * get_name() const
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Container class that returns results from GradientEvaluation. It contains the function value as well ...
virtual SGVector< float64_t > get_value()
virtual EEvaluationResultType get_result_type() const =0
T * data() const
Definition: SGVector.h:118
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:36

SHOGUN Machine Learning Toolbox - Documentation