SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Factor.cpp
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 Shell Hu
8  * Copyright (C) 2013 Shell Hu
9  */
10 
12 #include <shogun/base/Parameter.h>
13 
14 using namespace shogun;
15 
17 {
18  SG_UNSTABLE("CFactor::CFactor()", "\n");
19  init();
20 }
21 
23  SGVector<int32_t> var_index,
25 {
26  init();
27  m_factor_type = ftype;
28  m_var_index = var_index;
29  m_data = data;
30  m_is_data_dep = true;
31 
32  ASSERT(m_factor_type != NULL);
34 
35  if (m_data.size() == 0)
36  m_is_data_dep = false;
37 
38  if (ftype->is_table() && m_is_data_dep)
40 
43 }
44 
46  SGVector<int32_t> var_index,
47  SGSparseVector<float64_t> data_sparse) : CSGObject()
48 {
49  init();
50  m_factor_type = ftype;
51  m_var_index = var_index;
52  m_data_sparse = data_sparse;
53  m_is_data_dep = true;
54 
55  ASSERT(m_factor_type != NULL);
57 
59  m_is_data_dep = false;
60 
61  if (ftype->is_table() && m_is_data_dep)
63 
66 }
67 
69  SGVector<int32_t> var_index,
70  CFactorDataSource* data_source) : CSGObject()
71 {
72  init();
73  m_factor_type = ftype;
74  m_var_index = var_index;
75  m_data_source = data_source;
76  m_is_data_dep = true;
77 
78  ASSERT(m_factor_type != NULL);
80  ASSERT(m_data_source != NULL);
81 
82  if (ftype->is_table())
84 
87 }
88 
90 {
93 }
94 
96 {
98  return m_factor_type;
99 }
100 
102 {
103  m_factor_type = ftype;
105 }
106 
108 {
109  return m_var_index;
110 }
111 
112 const int32_t CFactor::get_num_vars() const
113 {
114  return m_var_index.size();
115 }
116 
118 {
119  m_var_index = vars.clone();
120 }
121 
123 {
125 }
126 
128 {
129  if (m_data_source != NULL)
130  return m_data_source->get_data();
131 
132  return m_data;
133 }
134 
136 {
137  if (m_data_source != NULL)
138  return m_data_source->get_data_sparse();
139 
140  return m_data_sparse;
141 }
142 
144 {
145  m_data = data_dense.clone();
146  m_is_data_dep = true;
147 }
148 
150  int32_t dlen)
151 {
152  m_data_sparse = SGSparseVector<float64_t>(data_sparse, dlen);
153  m_is_data_dep = true;
154 }
155 
157 {
158  return m_is_data_dep;
159 }
160 
162 {
163  if (m_data_source != NULL)
164  return m_data_source->is_sparse();
165 
166  return (m_data.size() == 0);
167 }
168 
170 {
171  if (is_data_dependent() == false && m_energies.size() == 0)
172  {
173  const SGVector<float64_t> ft_energies = m_factor_type->get_w();
174  ASSERT(ft_energies.size() == m_factor_type->get_num_assignments());
175  return ft_energies;
176  }
177  return m_energies;
178 }
179 
180 float64_t CFactor::get_energy(int32_t index) const
181 {
182  return get_energies()[index]; // note for data indep, we get m_w not m_energies
183 }
184 
186 {
187  REQUIRE(m_factor_type->get_num_assignments() == ft_energies.size(),
188  "%s::set_energies(): ft_energies is not a valid energy table!\n", get_name());
189 
190  m_energies = ft_energies;
191 }
192 
193 void CFactor::set_energy(int32_t ei, float64_t value)
194 {
195  REQUIRE(ei >= 0 && ei < m_factor_type->get_num_assignments(),
196  "%s::set_energy(): ei is out of index!\n", get_name());
197 
198  REQUIRE(is_data_dependent(), "%s::set_energy(): \
199  energy table is fixed in data dependent factor!\n", get_name());
200 
201  m_energies[ei] = value;
202 }
203 
205 {
207  return get_energy(index);
208 }
209 
211 {
212  if (is_data_dependent() == false)
213  return;
214 
215  // For some factor types the size of the energy table is determined only
216  // after an initialization step from training data.
217  if (m_energies.size() == 0)
219 
220  const SGVector<float64_t> H = get_data();
221  const SGSparseVector<float64_t> H_sparse = get_data_sparse();
222 
223  if (H_sparse.num_feat_entries == 0)
225  else
227 }
228 
230  const SGVector<float64_t> marginals,
231  SGVector<float64_t>& parameter_gradient,
232  float64_t mult) const
233 {
234  const SGVector<float64_t> H = get_data();
235  const SGSparseVector<float64_t> H_sparse = get_data_sparse();
236 
237  if (H_sparse.num_feat_entries == 0)
238  m_factor_type->compute_gradients(H, marginals, parameter_gradient, mult);
239  else
240  m_factor_type->compute_gradients(H_sparse, marginals, parameter_gradient, mult);
241 }
242 
243 void CFactor::init()
244 {
245  SG_ADD((CSGObject**)&m_factor_type, "type_name", "Factor type name", MS_NOT_AVAILABLE);
246  SG_ADD(&m_var_index, "var_index", "Factor variable index", MS_NOT_AVAILABLE);
247  SG_ADD(&m_energies, "energies", "Factor energies", MS_NOT_AVAILABLE);
248  SG_ADD((CSGObject**)&m_data_source, "data_source", "Factor data source", MS_NOT_AVAILABLE);
249  SG_ADD(&m_data, "data", "Factor data", MS_NOT_AVAILABLE);
250  SG_ADD(&m_data_sparse, "data_sparse", "Sparse factor data", MS_NOT_AVAILABLE);
251  SG_ADD(&m_is_data_dep, "is_data_dep", "Factor is data dependent or not", MS_NOT_AVAILABLE);
252 
253  m_factor_type=NULL;
254  m_data_source=NULL;
255  m_is_data_dep = false;
256 }
257 
259 {
260  init();
261 }
262 
264  : CSGObject()
265 {
266  init();
267  m_dense = dense;
268 }
269 
271  : CSGObject()
272 {
273  init();
274  m_sparse = sparse;
275 }
276 
278 {
279 }
280 
282 {
283  return (m_dense.size() == 0);
284 }
285 
287 {
288  return m_dense;
289 }
290 
292 {
293  return m_sparse;
294 }
295 
297 {
298  m_dense = dense.clone();
299 }
300 
302  int32_t dlen)
303 {
304  m_sparse = SGSparseVector<float64_t>(sparse, dlen);
305 }
306 
307 void CFactorDataSource::init()
308 {
309  SG_ADD(&m_dense, "dense", "Shared data", MS_NOT_AVAILABLE);
310  SG_ADD(&m_sparse, "sparse", "Shared sparse data", MS_NOT_AVAILABLE);
311 }
312 
void set_energy(int32_t ei, float64_t value)
Definition: Factor.cpp:193
bool is_data_sparse() const
Definition: Factor.cpp:161
float64_t get_energy(int32_t index) const
Definition: Factor.cpp:180
CTableFactorType * get_factor_type() const
Definition: Factor.cpp:95
SGSparseVector< float64_t > get_data_sparse() const
Definition: Factor.cpp:135
virtual SGVector< float64_t > get_data() const
Definition: Factor.cpp:286
virtual void compute_energies(const SGVector< float64_t > factor_data, SGVector< float64_t > &energies) const
Definition: FactorType.cpp:206
const SGVector< int32_t > get_variables() const
Definition: Factor.cpp:107
virtual SGVector< float64_t > get_w()
Definition: FactorType.cpp:77
void set_variables(SGVector< int32_t > vars)
Definition: Factor.cpp:117
virtual bool is_table() const
Definition: FactorType.h:143
void compute_energies()
Definition: Factor.cpp:210
#define REQUIRE(x,...)
Definition: SGIO.h:206
SGVector< float64_t > get_data() const
Definition: Factor.cpp:127
virtual const char * get_name() const
Definition: Factor.h:125
virtual SGSparseVector< float64_t > get_data_sparse() const
Definition: Factor.cpp:291
void compute_gradients(const SGVector< float64_t > marginals, SGVector< float64_t > &parameter_gradient, double mult=1.0) const
Definition: Factor.cpp:229
float64_t evaluate_energy(const SGVector< int32_t > state) const
Definition: Factor.cpp:204
#define SG_REF(x)
Definition: SGObject.h:54
virtual void set_data_sparse(SGSparseVectorEntry< float64_t > *sparse, int32_t dlen)
Definition: Factor.cpp:301
bool m_is_data_dep
Definition: Factor.h:236
SGVector< float64_t > get_energies() const
Definition: Factor.cpp:169
bool is_data_dependent() const
Definition: Factor.cpp:156
int32_t size() const
Definition: SGVector.h:113
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
SGSparseVector< float64_t > m_data_sparse
Definition: Factor.h:233
SGVector< int32_t > m_var_index
Definition: Factor.h:221
virtual void compute_gradients(const SGVector< float64_t > factor_data, const SGVector< float64_t > marginals, SGVector< float64_t > &parameter_gradient, double mult) const
Definition: FactorType.cpp:276
double float64_t
Definition: common.h:50
virtual ~CFactor()
Definition: Factor.cpp:89
Class CFactorDataSource Source for factor data. In some cases, the same data can be shared by many fa...
Definition: Factor.h:27
virtual void set_data(SGVector< float64_t > dense)
Definition: Factor.cpp:296
const int32_t get_num_vars() const
Definition: Factor.cpp:112
void set_energies(SGVector< float64_t > ft_energies)
Definition: Factor.cpp:185
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void set_data_sparse(SGSparseVectorEntry< float64_t > *data_sparse, int32_t dlen)
Definition: Factor.cpp:149
virtual bool is_sparse() const
Definition: Factor.cpp:281
Class CTableFactorType the way that store assignments of variables and energies in a table or a multi...
Definition: FactorType.h:122
void set_factor_type(CTableFactorType *ftype)
Definition: Factor.cpp:101
SGVector< T > clone() const
Definition: SGVector.cpp:207
virtual const SGVector< int32_t > get_cardinalities() const
Definition: FactorType.cpp:97
virtual ~CFactorDataSource()
Definition: Factor.cpp:277
void resize_vector(int32_t n)
Definition: SGVector.cpp:257
CTableFactorType * m_factor_type
Definition: Factor.h:218
#define SG_ADD(...)
Definition: SGObject.h:84
int32_t index_from_universe_assignment(const SGVector< int32_t > assig, const SGVector< int32_t > var_index) const
Definition: FactorType.cpp:190
#define SG_UNSTABLE(func,...)
Definition: SGIO.h:132
void set_data(SGVector< float64_t > data_dense)
Definition: Factor.cpp:143
const SGVector< int32_t > get_cardinalities() const
Definition: Factor.cpp:122
SGVector< float64_t > m_data
Definition: Factor.h:230
virtual int32_t get_num_assignments() const
Definition: FactorType.cpp:122
CFactorDataSource * m_data_source
Definition: Factor.h:227
SGVector< float64_t > m_energies
Definition: Factor.h:224

SHOGUN Machine Learning Toolbox - Documentation