SHOGUN  4.2.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 #include <shogun/base/Parameter.h>
19 
20 namespace shogun
21 {
22 
27 {
28 public:
31  {
32  m_total_variables=0;
33  m_gradient=NULL;
34  m_parameter_dictionary=NULL;
35  }
36 
37  virtual ~CGradientResult()
38  {
39  SG_UNREF(m_gradient);
40  SG_UNREF(m_parameter_dictionary);
41  }
42 
47  virtual const char* get_name() const { return "GradientResult"; }
48 
56  {
57  ASSERT(eval_result);
58 
60  "Provided evaluation result is not of type CGradientResult!\n")
61 
62  SG_REF(eval_result);
63  return (CGradientResult*) eval_result;
64  }
65 
71  {
73  }
74 
76  virtual void print_result()
77  {
78  REQUIRE(m_gradient, "Gradient map should not be NULL\n")
79  REQUIRE(m_parameter_dictionary, "Parameter dictionary should not be "
80  "NULL\n")
81 
82  // print value of the function
83  SG_SPRINT("Value: [")
84 
85  for (index_t i=0; i<m_value.vlen-1; i++)
86  SG_SPRINT("%f, ", m_value[i])
87 
88  if (m_value.vlen>0)
89  SG_SPRINT("%f", m_value[m_value.vlen-1])
90 
91  SG_SPRINT("] ")
92 
93  // print gradient wrt parameters
94  SG_SPRINT("Gradient: [")
95 
96  for (index_t i=0; i<m_gradient->get_num_elements(); i++)
97  {
98  CMapNode<TParameter*, SGVector<float64_t> >* param_node=
99  m_gradient->get_node_ptr(i);
100 
101  // get parameter name
102  const char* param_name=param_node->key->m_name;
103 
104  // get object name
105  const char* object_name=
106  m_parameter_dictionary->get_element(param_node->key)->get_name();
107 
108  // get gradient wrt parameter
109  SGVector<float64_t> param_gradient=param_node->data;
110 
111  SG_PRINT("%s.%s: ", object_name, param_name)
112 
113  for (index_t j=0; j<param_gradient.vlen-1; j++)
114  SG_SPRINT("%f, ", param_gradient[j])
115 
116  if (i==m_gradient->get_num_elements()-1)
117  {
118  if (param_gradient.vlen>0)
119  SG_PRINT("%f", param_gradient[param_gradient.vlen-1])
120  }
121  else
122  {
123  if (param_gradient.vlen>0)
124  SG_PRINT("%f; ", param_gradient[param_gradient.vlen-1])
125  }
126  }
127 
128  SG_SPRINT("] Total Variables: %u\n", m_total_variables)
129  }
130 
135  virtual uint32_t get_total_variables()
136  {
137  return m_total_variables;
138  }
139 
144  virtual void set_value(SGVector<float64_t> value)
145  {
146  m_value=SGVector<float64_t>(value);
147  }
148 
154  {
155  return SGVector<float64_t>(m_value);
156  }
157 
162  virtual void set_gradient(CMap<TParameter*, SGVector<float64_t> >* gradient)
163  {
164  REQUIRE(gradient, "Gradient map should not be NULL\n")
165 
166  SG_REF(gradient);
167  SG_UNREF(m_gradient);
168  m_gradient=gradient;
169 
170  m_total_variables=0;
171 
172  for (index_t i=0; i<gradient->get_num_elements(); i++)
173  {
174  CMapNode<TParameter*, SGVector<float64_t> >* node=
175  m_gradient->get_node_ptr(i);
176  m_total_variables+=node->data.vlen;
177  }
178  }
179 
185  {
186  SG_REF(m_gradient);
187  return m_gradient;
188  }
189 
195  CMap<TParameter*, CSGObject*>* parameter_dictionary)
196  {
197  SG_REF(parameter_dictionary);
198  SG_UNREF(m_parameter_dictionary);
199  m_parameter_dictionary=parameter_dictionary;
200  }
201 
207  {
208  SG_REF(m_parameter_dictionary);
209  return m_parameter_dictionary;
210  }
211 
212 private:
214  SGVector<float64_t> m_value;
215 
218 
220  CMap<TParameter*, CSGObject*>* m_parameter_dictionary;
221 
223  uint32_t m_total_variables;
224 };
225 }
226 #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:54
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:55
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:116
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:39

SHOGUN Machine Learning Toolbox - Documentation