SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
structure
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
11
#include <
shogun/structure/Factor.h
>
12
#include <
shogun/base/Parameter.h
>
13
14
using namespace
shogun;
15
16
CFactor::CFactor
() :
CSGObject
()
17
{
18
SG_UNSTABLE
(
"CFactor::CFactor()"
,
"\n"
);
19
init();
20
}
21
22
CFactor::CFactor
(
CTableFactorType
* ftype,
23
SGVector<int32_t>
var_index,
24
SGVector<float64_t>
data) :
CSGObject
()
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);
33
ASSERT
(
m_factor_type
->
get_cardinalities
().
size
() ==
m_var_index
.
size
());
34
35
if
(
m_data
.
size
() == 0)
36
m_is_data_dep
=
false
;
37
38
if
(ftype->
is_table
() &&
m_is_data_dep
)
39
m_energies
.
resize_vector
(ftype->
get_num_assignments
());
40
41
SG_REF
(
m_factor_type
);
42
SG_REF
(
m_data_source
);
43
}
44
45
CFactor::CFactor
(
CTableFactorType
* ftype,
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);
56
ASSERT
(
m_factor_type
->
get_cardinalities
().
size
() ==
m_var_index
.
size
());
57
58
if
(
m_data_sparse
.
num_feat_entries
== 0)
59
m_is_data_dep
=
false
;
60
61
if
(ftype->
is_table
() &&
m_is_data_dep
)
62
m_energies
.
resize_vector
(ftype->
get_num_assignments
());
63
64
SG_REF
(
m_factor_type
);
65
SG_REF
(
m_data_source
);
66
}
67
68
CFactor::CFactor
(
CTableFactorType
* ftype,
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);
79
ASSERT
(
m_factor_type
->
get_cardinalities
().
size
() ==
m_var_index
.
size
());
80
ASSERT
(
m_data_source
!= NULL);
81
82
if
(ftype->
is_table
())
83
m_energies
.
resize_vector
(ftype->
get_num_assignments
());
84
85
SG_REF
(
m_factor_type
);
86
SG_REF
(
m_data_source
);
87
}
88
89
CFactor::~CFactor
()
90
{
91
SG_UNREF
(
m_factor_type
);
92
SG_UNREF
(
m_data_source
);
93
}
94
95
CTableFactorType
*
CFactor::get_factor_type
()
const
96
{
97
SG_REF
(
m_factor_type
);
98
return
m_factor_type
;
99
}
100
101
void
CFactor::set_factor_type
(
CTableFactorType
* ftype)
102
{
103
m_factor_type
= ftype;
104
SG_REF
(
m_factor_type
);
105
}
106
107
const
SGVector<int32_t>
CFactor::get_variables
()
const
108
{
109
return
m_var_index
;
110
}
111
112
void
CFactor::set_variables
(
SGVector<int32_t>
vars)
113
{
114
m_var_index
= vars.
clone
();
115
}
116
117
const
SGVector<int32_t>
CFactor::get_cardinalities
()
const
118
{
119
return
m_factor_type
->
get_cardinalities
();
120
}
121
122
SGVector<float64_t>
CFactor::get_data
()
const
123
{
124
if
(
m_data_source
!= NULL)
125
return
m_data_source
->
get_data
();
126
127
return
m_data
;
128
}
129
130
SGSparseVector<float64_t>
CFactor::get_data_sparse
()
const
131
{
132
if
(
m_data_source
!= NULL)
133
return
m_data_source
->
get_data_sparse
();
134
135
return
m_data_sparse
;
136
}
137
138
void
CFactor::set_data
(
SGVector<float64_t>
data_dense)
139
{
140
m_data
= data_dense.
clone
();
141
m_is_data_dep
=
true
;
142
}
143
144
void
CFactor::set_data_sparse
(
SGSparseVectorEntry<float64_t>
* data_sparse,
145
int32_t dlen)
146
{
147
m_data_sparse
=
SGSparseVector<float64_t>
(data_sparse, dlen);
148
m_is_data_dep
=
true
;
149
}
150
151
bool
CFactor::is_data_dependent
()
const
152
{
153
return
m_is_data_dep
;
154
}
155
156
bool
CFactor::is_data_sparse
()
const
157
{
158
if
(
m_data_source
!= NULL)
159
return
m_data_source
->
is_sparse
();
160
161
return
(
m_data
.
size
() == 0);
162
}
163
164
SGVector<float64_t>
CFactor::get_energies
()
const
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
175
SGVector<float64_t>
CFactor::get_energies
()
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
192
void
CFactor::set_energies
(
SGVector<float64_t>
ft_energies)
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
211
float64_t
CFactor::evaluate_energy
(
const
SGVector<int32_t>
state)
const
212
{
213
int32_t index =
m_factor_type
->
index_from_universe_assignment
(state,
m_var_index
);
214
return
get_energy
(index);
215
}
216
217
void
CFactor::compute_energies
()
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)
225
m_energies
.
resize_vector
(
m_factor_type
->
get_num_assignments
());
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)
231
m_factor_type
->
compute_energies
(H,
m_energies
);
232
else
233
m_factor_type
->
compute_energies
(H_sparse,
m_energies
);
234
}
235
236
void
CFactor::compute_gradients
(
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
265
CFactorDataSource::CFactorDataSource
() :
CSGObject
()
266
{
267
init();
268
}
269
270
CFactorDataSource::CFactorDataSource
(
SGVector<float64_t>
dense)
271
:
CSGObject
()
272
{
273
init();
274
m_dense = dense;
275
}
276
277
CFactorDataSource::CFactorDataSource
(
SGSparseVector<float64_t>
sparse)
278
:
CSGObject
()
279
{
280
init();
281
m_sparse = sparse;
282
}
283
284
CFactorDataSource::~CFactorDataSource
()
285
{
286
}
287
288
bool
CFactorDataSource::is_sparse
()
const
289
{
290
return
(m_dense.
size
() == 0);
291
}
292
293
SGVector<float64_t>
CFactorDataSource::get_data
()
const
294
{
295
return
m_dense;
296
}
297
298
SGSparseVector<float64_t>
CFactorDataSource::get_data_sparse
()
const
299
{
300
return
m_sparse;
301
}
302
303
void
CFactorDataSource::set_data
(
SGVector<float64_t>
dense)
304
{
305
m_dense = dense.
clone
();
306
}
307
308
void
CFactorDataSource::set_data_sparse
(
SGSparseVectorEntry<float64_t>
* sparse,
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