SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
15 #include <shogun/lib/Map.h>
16 
17 namespace shogun
18 {
19 
24 {
25 public:
28  {
29  m_total_variables=0;
30  m_gradient=NULL;
31  m_parameter_dictionary=NULL;
32  }
33 
34  virtual ~CGradientResult()
35  {
36  SG_UNREF(m_gradient);
37  SG_UNREF(m_parameter_dictionary);
38  }
39 
44  virtual const char* get_name() const { return "GradientResult"; }
45 
53  {
54  ASSERT(eval_result);
55 
57  "Provided evaluation result is not of type CGradientResult!\n")
58 
59  SG_REF(eval_result);
60  return (CGradientResult*) eval_result;
61  }
62 
68  {
70  }
71 
73  virtual void print_result()
74  {
75  REQUIRE(m_gradient, "Gradient map should not be NULL\n")
76  REQUIRE(m_parameter_dictionary, "Parameter dictionary should not be "
77  "NULL\n")
78 
79  // print value of the function
80  SG_SPRINT("Value: [")
81 
82  for (index_t i=0; i<m_value.vlen-1; i++)
83  SG_SPRINT("%f, ", m_value[i])
84 
85  if (m_value.vlen>0)
86  SG_SPRINT("%f", m_value[m_value.vlen-1])
87 
88  SG_SPRINT("] ")
89 
90  // print gradient wrt parameters
91  SG_SPRINT("Gradient: [")
92 
93  for (index_t i=0; i<m_gradient->get_num_elements(); i++)
94  {
95  CMapNode<TParameter*, SGVector<float64_t> >* param_node=
96  m_gradient->get_node_ptr(i);
97 
98  // get parameter name
99  const char* param_name=param_node->key->m_name;
100 
101  // get object name
102  const char* object_name=
103  m_parameter_dictionary->get_element(param_node->key)->get_name();
104 
105  // get gradient wrt parameter
106  SGVector<float64_t> param_gradient=param_node->data;
107 
108  SG_PRINT("%s.%s: ", object_name, param_name)
109 
110  for (index_t j=0; j<param_gradient.vlen-1; j++)
111  SG_SPRINT("%f, ", param_gradient[j])
112 
113  if (i==m_gradient->get_num_elements()-1)
114  {
115  if (param_gradient.vlen>0)
116  SG_PRINT("%f", param_gradient[param_gradient.vlen-1])
117  }
118  else
119  {
120  if (param_gradient.vlen>0)
121  SG_PRINT("%f; ", param_gradient[param_gradient.vlen-1])
122  }
123  }
124 
125  SG_SPRINT("] Total Variables: %u\n", m_total_variables)
126  }
127 
132  virtual uint32_t get_total_variables()
133  {
134  return m_total_variables;
135  }
136 
141  virtual void set_value(SGVector<float64_t> value)
142  {
143  m_value=SGVector<float64_t>(value);
144  }
145 
151  {
152  return SGVector<float64_t>(m_value);
153  }
154 
159  virtual void set_gradient(CMap<TParameter*, SGVector<float64_t> >* gradient)
160  {
161  REQUIRE(gradient, "Gradient map should not be NULL\n")
162 
163  SG_REF(gradient);
164  SG_UNREF(m_gradient);
165  m_gradient=gradient;
166 
167  m_total_variables=0;
168 
169  for (index_t i=0; i<gradient->get_num_elements(); i++)
170  {
171  CMapNode<TParameter*, SGVector<float64_t> >* node=
172  m_gradient->get_node_ptr(i);
173  m_total_variables+=node->data.vlen;
174  }
175  }
176 
182  {
183  SG_REF(m_gradient);
184  return m_gradient;
185  }
186 
192  CMap<TParameter*, CSGObject*>* parameter_dictionary)
193  {
194  SG_REF(parameter_dictionary);
195  SG_UNREF(m_parameter_dictionary);
196  m_parameter_dictionary=parameter_dictionary;
197  }
198 
204  {
205  SG_REF(m_parameter_dictionary);
206  return m_parameter_dictionary;
207  }
208 
209 private:
211  SGVector<float64_t> m_value;
212 
215 
217  CMap<TParameter*, CSGObject*>* m_parameter_dictionary;
218 
220  uint32_t m_total_variables;
221 };
222 }
223 #endif /* CGRADIENTRESULT_H_ */

SHOGUN Machine Learning Toolbox - Documentation