SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
DualVariationalGaussianLikelihood.cpp
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Wu Lin
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  *
30  */
32 
33 #ifdef HAVE_EIGEN3
39 
40 using namespace Eigen;
41 
42 namespace shogun
43 {
44 
45 CDualVariationalGaussianLikelihood::CDualVariationalGaussianLikelihood()
47 {
48  init();
49 }
50 
52 {
53 }
54 
56 {
57  REQUIRE(m_likelihood, "The likelihood model must not be NULL\n");
59  REQUIRE(var_lik,
60  "The likelihood model (%s) does NOT support variational guassian inference\n",
62 
63  return var_lik;
64 }
65 
67 {
69  return var_lik->get_variational_expection();
70 }
71 
73 {
75  var_lik->set_noise_factor(noise_factor);
76 }
77 
79 {
81  return var_lik->get_variational_first_derivative(param);
82 }
83 
85 {
88 }
89 
91 {
93  return var_lik->get_first_derivative_wrt_hyperparameter(param);
94 }
95 
98 {
100  return var_lik->set_variational_distribution(mu, s2, lab);
101 }
102 
104 {
105  REQUIRE((strict_scale>0 && strict_scale<1),
106  "The strict_scale (%f) should be between 0 and 1 exclusively.\n",
107  strict_scale);
108  m_strict_scale=strict_scale;
109 }
110 
112 {
113  REQUIRE(direction.vlen==m_lambda.vlen,
114  "The length (%d) of direction should be same as the length (%d) of dual parameters\n",
115  direction.vlen, m_lambda.vlen);
116 
117  REQUIRE(step>=0,
118  "The step size (%f) should be non-negative\n", step);
119 
120  float64_t upper_bound=get_dual_upper_bound();
121  float64_t lower_bound=get_dual_lower_bound();
122 
123  ASSERT(upper_bound>=lower_bound);
124 
125  float64_t min_step=step;
126 
127  for (index_t i=0; i<direction.vlen; i++)
128  {
129  float64_t attempt=m_lambda[i]+step*direction[i];
130  float64_t adjust=0;
131 
132  if (direction[i]==0.0)
133  continue;
134 
135  if (lower_bound!=-CMath::INFTY && attempt<lower_bound)
136  {
137  adjust=(m_lambda[i]-lower_bound)/CMath::abs(direction[i]);
139  adjust*=(1-m_strict_scale);
140  if (adjust<min_step)
141  min_step=adjust;
142  }
143 
144  if (upper_bound!=CMath::INFTY && attempt>upper_bound)
145  {
146  adjust=(upper_bound-m_lambda[i])/CMath::abs(direction[i]);
148  adjust*=(1-m_strict_scale);
149  if (adjust<min_step)
150  min_step=adjust;
151  }
152  }
153 
154  return min_step;
155 }
156 
158 {
159  REQUIRE(lab, "Labels are required (lab should not be NULL)\n");
160 
161  REQUIRE((lambda.vlen==lab->get_num_labels()),
162  "Length of the vector of lambda (%d) "
163  "and number of labels (%d) should be the same\n",
164  lambda.vlen, lab->get_num_labels());
166  "Labels (%s) must be type of CBinaryLabels\n",
167  lab->get_name());
168 
169  m_lab=(((CBinaryLabels*)lab)->get_labels()).clone();
170 
171  //Convert the input label to standard label used in the class
172  //Note that Shogun uses -1 and 1 as labels and this class internally uses
173  //0 and 1 repectively.
174  for(index_t i = 0; i < m_lab.size(); ++i)
175  m_lab[i]=CMath::max(m_lab[i], 0.0);
176 
177  m_lambda=lambda;
178 
179  precompute();
180 }
181 
183 {
184  float64_t lower_bound=get_dual_lower_bound();
185  float64_t upper_bound=get_dual_upper_bound();
186 
187  for (index_t i=0; i<m_lambda.vlen; i++)
188  {
189  float64_t value=m_lambda[i];
190  if (value<lower_bound)
191  return false;
192  else
193  {
194  if (dual_lower_bound_strict() && value==lower_bound)
195  return false;
196  else
197  {
198  if (value>upper_bound)
199  return false;
200  else
201  {
202  if (dual_upper_bound_strict() && value==upper_bound)
203  return false;
204 
205  }
206  }
207  }
208 
209  }
210  return true;
211 }
212 
214 {
216 }
217 
218 void CDualVariationalGaussianLikelihood::init()
219 {
220  SG_ADD(&m_lambda, "lambda",
221  "Dual parameter for variational s2",
223 
224  SG_ADD(&m_is_valid, "is_valid",
225  "Is the Dual parameter valid",
227 
228  SG_ADD(&m_strict_scale, "strict_scale",
229  "The strict variable used in adjust_step_wrt_dual_parameter",
231 
232  m_is_valid=false;
233  m_strict_scale=1e-5;
234 }
235 
236 } /* namespace shogun */
237 #endif /* HAVE_EIGEN3 */
virtual const char * get_name() const =0
virtual SGVector< float64_t > get_first_derivative_wrt_hyperparameter(const TParameter *param) const
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual ELabelType get_label_type() const =0
binary labels +1/-1
Definition: LabelTypes.h:18
virtual SGVector< float64_t > get_first_derivative_wrt_hyperparameter(const TParameter *param) const =0
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const =0
virtual void set_dual_parameters(SGVector< float64_t > the_lambda, const CLabels *lab)
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
static const float64_t INFTY
infinity
Definition: Math.h:2048
virtual CSGObject * clone()
Definition: SGObject.cpp:714
virtual int32_t get_num_labels() const =0
virtual bool dual_lower_bound_strict() const =0
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
Definition: SGMatrix.h:20
parameter struct
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual void set_noise_factor(float64_t noise_factor)
virtual float64_t get_dual_upper_bound() const =0
virtual float64_t adjust_step_wrt_dual_parameter(SGVector< float64_t > direction, const float64_t step) const
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual SGVector< float64_t > get_variational_expection()=0
static T max(T a, T b)
Definition: Math.h:168
virtual CVariationalGaussianLikelihood * get_variational_likelihood() const
virtual float64_t get_dual_lower_bound() const =0
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The Variational Likelihood base class.
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual bool dual_upper_bound_strict() const =0
#define SG_ADD(...)
Definition: SGObject.h:81
virtual bool supports_derivative_wrt_hyperparameter() const =0
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const
static T abs(T a)
Definition: Math.h:179

SHOGUN 机器学习工具包 - 项目文档