SHOGUN  6.1.3
DualVariationalGaussianLikelihood.cpp
Go to the documentation of this file.
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 
38 
39 using namespace Eigen;
40 
41 namespace shogun
42 {
43 
44 CDualVariationalGaussianLikelihood::CDualVariationalGaussianLikelihood()
46 {
47  init();
48 }
49 
51 {
52 }
53 
55 {
56  REQUIRE(m_likelihood, "The likelihood model must not be NULL\n");
58  REQUIRE(var_lik,
59  "The likelihood model (%s) does NOT support variational guassian inference\n",
61 
62  return var_lik;
63 }
64 
66 {
68  return var_lik->get_variational_expection();
69 }
70 
72 {
74  var_lik->set_noise_factor(noise_factor);
75 }
76 
78 {
80  return var_lik->get_variational_first_derivative(param);
81 }
82 
84 {
87 }
88 
90 {
92  return var_lik->get_first_derivative_wrt_hyperparameter(param);
93 }
94 
97 {
99  return var_lik->set_variational_distribution(mu, s2, lab);
100 }
101 
103 {
104  REQUIRE((strict_scale>0 && strict_scale<1),
105  "The strict_scale (%f) should be between 0 and 1 exclusively.\n",
106  strict_scale);
107  m_strict_scale=strict_scale;
108 }
109 
111 {
112  REQUIRE(direction.vlen==m_lambda.vlen,
113  "The length (%d) of direction should be same as the length (%d) of dual parameters\n",
114  direction.vlen, m_lambda.vlen);
115 
116  REQUIRE(step>=0,
117  "The step size (%f) should be non-negative\n", step);
118 
119  float64_t upper_bound=get_dual_upper_bound();
120  float64_t lower_bound=get_dual_lower_bound();
121 
122  ASSERT(upper_bound>=lower_bound);
123 
124  float64_t min_step=step;
125 
126  for (index_t i=0; i<direction.vlen; i++)
127  {
128  float64_t attempt=m_lambda[i]+step*direction[i];
129  float64_t adjust=0;
130 
131  if (direction[i]==0.0)
132  continue;
133 
134  if (lower_bound!=-CMath::INFTY && attempt<lower_bound)
135  {
136  adjust=(m_lambda[i]-lower_bound)/CMath::abs(direction[i]);
138  adjust*=(1-m_strict_scale);
139  if (adjust<min_step)
140  min_step=adjust;
141  }
142 
143  if (upper_bound!=CMath::INFTY && attempt>upper_bound)
144  {
145  adjust=(upper_bound-m_lambda[i])/CMath::abs(direction[i]);
147  adjust*=(1-m_strict_scale);
148  if (adjust<min_step)
149  min_step=adjust;
150  }
151  }
152 
153  return min_step;
154 }
155 
157 {
158  REQUIRE(lab, "Labels are required (lab should not be NULL)\n");
159 
160  REQUIRE((lambda.vlen==lab->get_num_labels()),
161  "Length of the vector of lambda (%d) "
162  "and number of labels (%d) should be the same\n",
163  lambda.vlen, lab->get_num_labels());
165  "Labels (%s) must be type of CBinaryLabels\n",
166  lab->get_name());
167 
168  m_lab=(((CBinaryLabels*)lab)->get_labels()).clone();
169 
170  //Convert the input label to standard label used in the class
171  //Note that Shogun uses -1 and 1 as labels and this class internally uses
172  //0 and 1 repectively.
173  for(index_t i = 0; i < m_lab.size(); ++i)
174  m_lab[i]=CMath::max(m_lab[i], 0.0);
175 
176  m_lambda=lambda;
177 
178  precompute();
179 }
180 
182 {
183  float64_t lower_bound=get_dual_lower_bound();
184  float64_t upper_bound=get_dual_upper_bound();
185 
186  for (index_t i=0; i<m_lambda.vlen; i++)
187  {
188  float64_t value=m_lambda[i];
189  if (value<lower_bound)
190  return false;
191  else
192  {
193  if (dual_lower_bound_strict() && value==lower_bound)
194  return false;
195  else
196  {
197  if (value>upper_bound)
198  return false;
199  else
200  {
201  if (dual_upper_bound_strict() && value==upper_bound)
202  return false;
203 
204  }
205  }
206  }
207 
208  }
209  return true;
210 }
211 
213 {
215 }
216 
217 void CDualVariationalGaussianLikelihood::init()
218 {
219  SG_ADD(&m_lambda, "lambda",
220  "Dual parameter for variational s2",
222 
223  SG_ADD(&m_is_valid, "is_valid",
224  "Is the Dual parameter valid",
226 
227  SG_ADD(&m_strict_scale, "strict_scale",
228  "The strict variable used in adjust_step_wrt_dual_parameter",
230 
231  m_is_valid=false;
232  m_strict_scale=1e-5;
233 }
234 
235 } /* namespace shogun */
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
static T max(T a, T b)
Definition: Math.h:149
virtual void set_dual_parameters(SGVector< float64_t > the_lambda, const CLabels *lab)
int32_t index_t
Definition: common.h:72
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
static const float64_t INFTY
infinity
Definition: Math.h:1868
virtual CSGObject * clone()
Definition: SGObject.cpp:734
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:25
parameter struct
#define REQUIRE(x,...)
Definition: SGIO.h:181
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
#define ASSERT(x)
Definition: SGIO.h:176
double float64_t
Definition: common.h:60
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual SGVector< float64_t > get_variational_expection()=0
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:93
virtual bool supports_derivative_wrt_hyperparameter() const =0
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const
index_t vlen
Definition: SGVector.h:571
static T abs(T a)
Definition: Math.h:161

SHOGUN Machine Learning Toolbox - Documentation