SHOGUN
v2.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
regression
gp
InferenceMethod.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
* Copyright (C) 2012 Jacob Walker
8
*/
9
10
#include <
shogun/regression/gp/InferenceMethod.h
>
11
#ifdef HAVE_EIGEN3
12
#include <
shogun/mathematics/lapack.h
>
13
#include <
shogun/mathematics/Math.h
>
14
#include <
shogun/labels/RegressionLabels.h
>
15
#include <
shogun/kernel/GaussianKernel.h
>
16
#include <
shogun/features/CombinedFeatures.h
>
17
18
using namespace
shogun;
19
20
CInferenceMethod::CInferenceMethod
()
21
{
22
init();
23
24
m_kernel
= NULL;
25
m_model
= NULL;
26
m_labels
= NULL;
27
m_features
= NULL;
28
m_latent_features
= NULL;
29
m_mean
= NULL;
30
}
31
32
CInferenceMethod::CInferenceMethod
(
CKernel
* kern,
CFeatures
* feat,
33
CMeanFunction
* m,
CLabels
* lab,
CLikelihoodModel
* mod)
34
{
35
init();
36
37
set_kernel
(kern);
38
set_features
(feat);
39
set_labels
(lab);
40
set_model
(mod);
41
set_mean
(m);
42
}
43
44
CInferenceMethod::~CInferenceMethod
()
45
{
46
SG_UNREF
(
m_kernel
);
47
SG_UNREF
(
m_features
);
48
SG_UNREF
(
m_latent_features
);
49
SG_UNREF
(
m_labels
);
50
SG_UNREF
(
m_model
);
51
SG_UNREF
(
m_mean
);
52
}
53
54
void
CInferenceMethod::init()
55
{
56
SG_ADD
((
CSGObject
**)&
m_kernel
,
"kernel"
,
"Kernel"
,
MS_AVAILABLE
);
57
SG_ADD
(&
m_scale
,
"scale"
,
"Kernel Scale"
,
MS_AVAILABLE
);
58
SG_ADD
((
CSGObject
**)&
m_model
,
"likelihood_model"
,
"Likelihood model"
,
59
MS_AVAILABLE
);
60
SG_ADD
((
CSGObject
**)&
m_labels
,
"labels"
,
"Labels"
,
MS_NOT_AVAILABLE
);
61
SG_ADD
((
CSGObject
**)&
m_features
,
"features"
,
"Features"
,
MS_NOT_AVAILABLE
);
62
SG_ADD
((
CSGObject
**)&
m_latent_features
,
"latent_features"
,
"latent Features"
,
MS_NOT_AVAILABLE
);
63
SG_ADD
((
CSGObject
**)&
m_mean
,
"mean_function"
,
"Mean Function"
,
MS_NOT_AVAILABLE
);
64
65
m_kernel
= NULL;
66
m_model
= NULL;
67
m_labels
= NULL;
68
m_features
= NULL;
69
m_latent_features
= NULL;
70
m_mean
= NULL;
71
m_scale
= 1.0;
72
}
73
74
void
CInferenceMethod::set_features
(
CFeatures
* feat)
75
{
76
SG_REF
(feat);
77
SG_UNREF
(
m_features
);
78
m_features
=feat;
79
80
if
(
m_features
&&
m_features
->
has_property
(
FP_DOT
) &&
m_features
->
get_num_vectors
())
81
m_feature_matrix
=
82
((
CDotFeatures
*)
m_features
)->get_computed_dot_feature_matrix();
83
84
else
if
(
m_features
&&
m_features
->
get_feature_class
() ==
C_COMBINED
)
85
{
86
CDotFeatures
* subfeat =
87
(
CDotFeatures
*)((
CCombinedFeatures
*)
m_features
)->
88
get_first_feature_obj();
89
90
if
(
m_features
->
get_num_vectors
())
91
m_feature_matrix
= subfeat->
get_computed_dot_feature_matrix
();
92
93
SG_UNREF
(subfeat);
94
}
95
96
update_data_means
();
97
update_train_kernel
();
98
update_chol
();
99
update_alpha
();
100
}
101
102
void
CInferenceMethod::set_latent_features
(
CFeatures
* feat)
103
{
104
SG_REF
(feat);
105
SG_UNREF
(
m_latent_features
);
106
m_latent_features
=feat;
107
108
if
(
m_latent_features
&&
m_latent_features
->
has_property
(
FP_DOT
) &&
m_latent_features
->
get_num_vectors
())
109
m_latent_matrix
=
110
((
CDotFeatures
*)
m_latent_features
)->get_computed_dot_feature_matrix();
111
112
else
if
(
m_latent_features
&&
m_latent_features
->
get_feature_class
() ==
C_COMBINED
)
113
{
114
CDotFeatures
* subfeat =
115
(
CDotFeatures
*)((
CCombinedFeatures
*)
m_latent_features
)->
116
get_first_feature_obj();
117
118
if
(
m_latent_features
->
get_num_vectors
())
119
m_latent_matrix
= subfeat->
get_computed_dot_feature_matrix
();
120
121
SG_UNREF
(subfeat);
122
}
123
124
update_data_means
();
125
update_train_kernel
();
126
update_chol
();
127
update_alpha
();
128
}
129
130
void
CInferenceMethod::set_kernel
(
CKernel
* kern)
131
{
132
SG_REF
(kern);
133
SG_UNREF
(
m_kernel
);
134
m_kernel
= kern;
135
update_train_kernel
();
136
update_chol
();
137
update_alpha
();
138
}
139
140
void
CInferenceMethod::set_mean
(
CMeanFunction
* m)
141
{
142
SG_REF
(m);
143
SG_UNREF
(
m_mean
);
144
m_mean
= m;
145
146
update_data_means
();
147
update_chol
();
148
update_alpha
();
149
}
150
151
void
CInferenceMethod::set_labels
(
CLabels
* lab)
152
{
153
SG_REF
(lab);
154
SG_UNREF
(
m_labels
);
155
m_labels
= lab;
156
157
if
(
m_labels
)
158
{
159
m_label_vector
=
160
((
CRegressionLabels
*)
m_labels
)->get_labels().
clone
();
161
}
162
163
update_data_means
();
164
update_alpha
();
165
}
166
167
void
CInferenceMethod::set_model
(
CLikelihoodModel
* mod)
168
{
169
SG_REF
(mod);
170
SG_UNREF
(
m_model
);
171
m_model
= mod;
172
update_train_kernel
();
173
update_chol
();
174
update_alpha
();
175
}
176
177
void
CInferenceMethod::set_scale
(
float64_t
s)
178
{
179
update_train_kernel
();
180
m_scale
= s;
181
update_chol
();
182
update_alpha
();
183
}
184
185
void
CInferenceMethod::update_data_means
()
186
{
187
if
(
m_mean
)
188
{
189
m_data_means
=
190
m_mean
->
get_mean_vector
(
m_feature_matrix
);
191
192
193
if
(
m_label_vector
.
vlen
==
m_data_means
.
vlen
)
194
{
195
for
(
index_t
i = 0; i <
m_label_vector
.
vlen
; i++)
196
m_label_vector
[i] -=
m_data_means
[i];
197
}
198
}
199
}
200
#endif
/* HAVE_EIGEN3 */
SHOGUN
Machine Learning Toolbox - Documentation