SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
113 {
114  m_var_index = vars.clone();
115 }
116 
118 {
120 }
121 
123 {
124  if (m_data_source != NULL)
125  return m_data_source->get_data();
126 
127  return m_data;
128 }
129 
131 {
132  if (m_data_source != NULL)
133  return m_data_source->get_data_sparse();
134 
135  return m_data_sparse;
136 }
137 
139 {
140  m_data = data_dense.clone();
141  m_is_data_dep = true;
142 }
143 
145  int32_t dlen)
146 {
147  m_data_sparse = SGSparseVector<float64_t>(data_sparse, dlen);
148  m_is_data_dep = true;
149 }
150 
152 {
153  return m_is_data_dep;
154 }
155 
157 {
158  if (m_data_source != NULL)
159  return m_data_source->is_sparse();
160 
161  return (m_data.size() == 0);
162 }
163 
165 {
166  if (is_data_dependent() == false && m_energies.size() == 0)
167  {
168  const SGVector<float64_t> ft_energies = m_factor_type->get_w();
169  ASSERT(ft_energies.size() == m_factor_type->get_num_assignments());
170  return ft_energies;
171  }
172  return m_energies;
173 }
174 
176 {
177  if (is_data_dependent() == false && m_energies.size() == 0)
178  {
179  SGVector<float64_t> ft_energies
180  = const_cast<CTableFactorType*>(m_factor_type)->get_w();
181  ASSERT(ft_energies.size() == m_factor_type->get_num_assignments());
182  return ft_energies;
183  }
184  return m_energies;
185 }
186 
187 float64_t CFactor::get_energy(int32_t index) const
188 {
189  return get_energies()[index]; // note for data indep, we get m_w not m_energies
190 }
191 
193 {
194  REQUIRE(m_factor_type->get_num_assignments() == ft_energies.size(),
195  "%s::set_energies(): ft_energies is not a valid energy table!\n", get_name());
196 
197  m_energies = ft_energies;
198 }
199 
200 void CFactor::set_energy(int32_t ei, float64_t value)
201 {
202  REQUIRE(ei >= 0 && ei < m_factor_type->get_num_assignments(),
203  "%s::set_energy(): ei is out of index!\n", get_name());
204 
205  REQUIRE(is_data_dependent(), "%s::set_energy(): \
206  energy table is fixed in data dependent factor!\n", get_name());
207 
208  m_energies[ei] = value;
209 }
210 
212 {
214  return get_energy(index);
215 }
216 
218 {
219  if (is_data_dependent() == false)
220  return;
221 
222  // For some factor types the size of the energy table is determined only
223  // after an initialization step from training data.
224  if (m_energies.size() == 0)
226 
227  const SGVector<float64_t> H = get_data();
228  const SGSparseVector<float64_t> H_sparse = get_data_sparse();
229 
230  if (H_sparse.num_feat_entries == 0)
232  else
234 }
235 
237  const SGVector<float64_t> marginals,
238  SGVector<float64_t>& parameter_gradient,
239  float64_t mult) const
240 {
241  const SGVector<float64_t> H = get_data();
242  const SGSparseVector<float64_t> H_sparse = get_data_sparse();
243 
244  if (H_sparse.num_feat_entries == 0)
245  m_factor_type->compute_gradients(H, marginals, parameter_gradient, mult);
246  else
247  m_factor_type->compute_gradients(H_sparse, marginals, parameter_gradient, mult);
248 }
249 
250 void CFactor::init()
251 {
252  SG_ADD((CSGObject**)&m_factor_type, "type_name", "Factor type name", MS_NOT_AVAILABLE);
253  SG_ADD(&m_var_index, "var_index", "Factor variable index", MS_NOT_AVAILABLE);
254  SG_ADD(&m_energies, "energies", "Factor energies", MS_NOT_AVAILABLE);
255  SG_ADD((CSGObject**)&m_data_source, "data_source", "Factor data source", MS_NOT_AVAILABLE);
256  SG_ADD(&m_data, "data", "Factor data", MS_NOT_AVAILABLE);
257  SG_ADD(&m_data_sparse, "data_sparse", "Sparse factor data", MS_NOT_AVAILABLE);
258  SG_ADD(&m_is_data_dep, "is_data_dep", "Factor is data dependent or not", MS_NOT_AVAILABLE);
259 
260  m_factor_type=NULL;
261  m_data_source=NULL;
262  m_is_data_dep = false;
263 }
264 
266 {
267  init();
268 }
269 
271  : CSGObject()
272 {
273  init();
274  m_dense = dense;
275 }
276 
278  : CSGObject()
279 {
280  init();
281  m_sparse = sparse;
282 }
283 
285 {
286 }
287 
289 {
290  return (m_dense.size() == 0);
291 }
292 
294 {
295  return m_dense;
296 }
297 
299 {
300  return m_sparse;
301 }
302 
304 {
305  m_dense = dense.clone();
306 }
307 
309  int32_t dlen)
310 {
311  m_sparse = SGSparseVector<float64_t>(sparse, dlen);
312 }
313 
314 void CFactorDataSource::init()
315 {
316  SG_ADD(&m_dense, "dense", "Shared data", MS_NOT_AVAILABLE);
317  SG_ADD(&m_sparse, "sparse", "Shared sparse data", MS_NOT_AVAILABLE);
318 }
319 

SHOGUN Machine Learning Toolbox - Documentation