SHOGUN  3.2.1
 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 
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_ */

SHOGUN Machine Learning Toolbox - Documentation