SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
structure
Factor.cpp
浏览该文件的文档.
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
float64_t
CFactor::get_energy
(int32_t index)
const
176
{
177
return
get_energies
()[index];
// note for data indep, we get m_w not m_energies
178
}
179
180
void
CFactor::set_energies
(
SGVector<float64_t>
ft_energies)
181
{
182
REQUIRE
(
m_factor_type
->
get_num_assignments
() == ft_energies.
size
(),
183
"%s::set_energies(): ft_energies is not a valid energy table!\n"
,
get_name
());
184
185
m_energies
= ft_energies;
186
}
187
188
void
CFactor::set_energy
(int32_t ei,
float64_t
value)
189
{
190
REQUIRE
(ei >= 0 && ei < m_factor_type->get_num_assignments(),
191
"%s::set_energy(): ei is out of index!\n"
,
get_name
());
192
193
REQUIRE
(
is_data_dependent
(),
"%s::set_energy(): \
194
energy table is fixed in data dependent factor!\n"
,
get_name
());
195
196
m_energies
[ei] = value;
197
}
198
199
float64_t
CFactor::evaluate_energy
(
const
SGVector<int32_t>
state)
const
200
{
201
int32_t index =
m_factor_type
->
index_from_universe_assignment
(state,
m_var_index
);
202
return
get_energy
(index);
203
}
204
205
void
CFactor::compute_energies
()
206
{
207
if
(
is_data_dependent
() ==
false
)
208
return
;
209
210
// For some factor types the size of the energy table is determined only
211
// after an initialization step from training data.
212
if
(
m_energies
.
size
() == 0)
213
m_energies
.
resize_vector
(
m_factor_type
->
get_num_assignments
());
214
215
const
SGVector<float64_t>
H
=
get_data
();
216
const
SGSparseVector<float64_t>
H_sparse =
get_data_sparse
();
217
218
if
(H_sparse.
num_feat_entries
== 0)
219
m_factor_type
->
compute_energies
(H,
m_energies
);
220
else
221
m_factor_type
->
compute_energies
(H_sparse,
m_energies
);
222
}
223
224
void
CFactor::compute_gradients
(
225
const
SGVector<float64_t>
marginals,
226
SGVector<float64_t>
& parameter_gradient,
227
float64_t
mult)
const
228
{
229
const
SGVector<float64_t>
H
=
get_data
();
230
const
SGSparseVector<float64_t>
H_sparse =
get_data_sparse
();
231
232
if
(H_sparse.
num_feat_entries
== 0)
233
m_factor_type
->
compute_gradients
(H, marginals, parameter_gradient, mult);
234
else
235
m_factor_type
->
compute_gradients
(H_sparse, marginals, parameter_gradient, mult);
236
}
237
238
void
CFactor::init()
239
{
240
SG_ADD
((
CSGObject
**)&
m_factor_type
,
"type_name"
,
"Factor type name"
,
MS_NOT_AVAILABLE
);
241
SG_ADD
(&
m_var_index
,
"var_index"
,
"Factor variable index"
,
MS_NOT_AVAILABLE
);
242
SG_ADD
(&
m_energies
,
"energies"
,
"Factor energies"
,
MS_NOT_AVAILABLE
);
243
SG_ADD
((
CSGObject
**)&
m_data_source
,
"data_source"
,
"Factor data source"
,
MS_NOT_AVAILABLE
);
244
SG_ADD
(&
m_data
,
"data"
,
"Factor data"
,
MS_NOT_AVAILABLE
);
245
SG_ADD
(&
m_data_sparse
,
"data_sparse"
,
"Sparse factor data"
,
MS_NOT_AVAILABLE
);
246
SG_ADD
(&
m_is_data_dep
,
"is_data_dep"
,
"Factor is data dependent or not"
,
MS_NOT_AVAILABLE
);
247
248
m_factor_type
=NULL;
249
m_data_source
=NULL;
250
m_is_data_dep
=
false
;
251
}
252
253
CFactorDataSource::CFactorDataSource
() :
CSGObject
()
254
{
255
init();
256
}
257
258
CFactorDataSource::CFactorDataSource
(
SGVector<float64_t>
dense)
259
:
CSGObject
()
260
{
261
init();
262
m_dense = dense;
263
}
264
265
CFactorDataSource::CFactorDataSource
(
SGSparseVector<float64_t>
sparse)
266
:
CSGObject
()
267
{
268
init();
269
m_sparse = sparse;
270
}
271
272
CFactorDataSource::~CFactorDataSource
()
273
{
274
}
275
276
bool
CFactorDataSource::is_sparse
()
const
277
{
278
return
(m_dense.
size
() == 0);
279
}
280
281
SGVector<float64_t>
CFactorDataSource::get_data
()
const
282
{
283
return
m_dense;
284
}
285
286
SGSparseVector<float64_t>
CFactorDataSource::get_data_sparse
()
const
287
{
288
return
m_sparse;
289
}
290
291
void
CFactorDataSource::set_data
(
SGVector<float64_t>
dense)
292
{
293
m_dense = dense.
clone
();
294
}
295
296
void
CFactorDataSource::set_data_sparse
(
SGSparseVectorEntry<float64_t>
* sparse,
297
int32_t dlen)
298
{
299
m_sparse =
SGSparseVector<float64_t>
(sparse, dlen);
300
}
301
302
void
CFactorDataSource::init()
303
{
304
SG_ADD
(&m_dense,
"dense"
,
"Shared data"
,
MS_NOT_AVAILABLE
);
305
SG_ADD
(&m_sparse,
"sparse"
,
"Shared sparse data"
,
MS_NOT_AVAILABLE
);
306
}
307
SHOGUN
机器学习工具包 - 项目文档